Running Blazor Web App

My web site is running on Linux/Apache server. So, I wanted Blazor Web App to run on that server too.

Developing Blazor app was quite easy and straight forward process but writing the right .htaccess file was not so easy. I searched the internet and found sample on Microsoft web pages (https://docs.microsoft.com/en-us/aspnet/core/blazor/host-and-deploy/?view=aspnetcore-6.0&tabs=visual-studio), but provided .htaccess file sample was not enough.

I think that running Blazor on Apache is interesting for others too, so here is .htaccess file I am using in production (just update string ‘sub-directory’ (2x) with correct directory path of your app on Apache file system).

Link to GitHub gist: Apache .htaccess file for running Blazor Web App (github.com)

<IfModule mod_headers.c>
    # Serve brotli compressed files if they exist and the client accepts brotli.
    RewriteCond "%{HTTP:Accept-encoding}" "br"
    RewriteCond "%{REQUEST_FILENAME}\.br" "-s"
    RewriteRule "^(.*)\.(js|json|css|dll|dat|blat|wasm)$" "$1\.$2\.br" [QSA]

    # Serve gzip compressed files if they exist and the client accepts gzip.
    RewriteCond "%{HTTP:Accept-encoding}" "gz"
    RewriteCond "%{REQUEST_FILENAME}\.gz" "-s"
    RewriteRule "^(.*)\.(js|json|css|dll|dat|blat|wasm)$" "$1\.$2\.gz" [QSA]
    
    # Serve correct content types, and prevent double compression.
    RewriteRule "\.css\.br$" "-" [T=text/css,E=no-brotli:1]
    RewriteRule "\.js\.br$"  "-" [T=text/javascript,E=no-brotli:1]
    RewriteRule "\.json\.br$"  "-" [T=application/json,E=no-brotli:1]
    RewriteRule "\.dll\.br$"  "-" [T=T=application/octet-stream,E=no-brotli:1]  
    RewriteRule "\.dat\.br$"  "-" [T=application/octet-stream,E=no-brotli:1]  
    RewriteRule "\.blat\.br$"  "-" [T=application/octet-stream,E=no-brotli:1]  
    RewriteRule "\.wasm\.br$"  "-" [T=application/wasm,E=no-brotli:1]  
    
    # Serve correct content types, and prevent double compression.
    RewriteRule "\.css\.gz$" "-" [T=text/css,E=no-gzip:1]
    RewriteRule "\.js\.gz$"  "-" [T=text/javascript,E=no-gzip:1]
    RewriteRule "\.json\.gz$"  "-" [T=application/json,E=no-gzip:1]
    RewriteRule "\.dll\.gz$"  "-" [T=T=application/octet-stream,E=no-gzip:1]  
    RewriteRule "\.dat\.gz$"  "-" [T=application/octet-stream,E=no-gzip:1]  
    RewriteRule "\.blat\.gz$"  "-" [T=application/octet-stream,E=no-gzip:1]  
    RewriteRule "\.wasm\.gz$"  "-" [T=application/wasm,E=no-gzip:1]  
    
    <FilesMatch "(\.js\.br|\.css\.br|\.json\.br|\.dll\.br|\.dat\.br|\.blat\.br|\.wasm\.br)$">
      # Serve correct encoding type.
      Header set Content-Encoding br
      # Force proxies to cache brotli & non-brotli
      Header append Vary Accept-Encoding
    </FilesMatch>
    
    <FilesMatch "(\.js\.gz|\.css\.gz|\.json\.gz|\.dll\.gz|\.dat\.gz|\.blat\.gz|\.wasm\.gz)$">
      # Serve correct encoding type.
      Header set Content-Encoding gzip
      # Force proxies to cache gzipped & non-gzipped
      Header append Vary Accept-Encoding
    </FilesMatch>
</IfModule>

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /sub-directory/
  RewriteRule ^index\.html$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . /sub-directory/index.html [L]
</IfModule>

Komentirajte prispevek

This site uses Akismet to reduce spam. Learn how your comment data is processed.