Search code examples
.htaccessgzipminify

Sending pre-minified/pre-gzipped CSS and JS to the browser


I have automatic way of minify/gzip JS and CSS during deploy on the server. Files are really lite. Now I'm having a problem, browser doesn't read it. I've tried something like this in .htaccess:

<FilesMatch .*\.min.css.gz>
ForceType text/css
</FilesMatch>

<FilesMatch .*\.min.js.gz>
ForceType text/javascript
</FilesMatch>

What I have to do to make it work and not to infect on slowing server down? Thanks in advance


Solution

  • I figure it, here is the solution:

    AddEncoding gzip .jsgz .cssgz
    AddType application/x-javascript .jsgz
    AddType text/css .cssgz
    
    RewriteEngine on 
    # If client accepts compressed files 
    RewriteCond %{HTTP:Accept-Encoding} gzip 
    # and if compressed file exists 
    RewriteCond %{REQUEST_FILENAME}gz -f 
    # send .min.cssgz and .min.cssgz instead of .css and .js
    RewriteRule ^(.+)\.(min.css|min.js)$ $1.$2gz [L]
    

    For some reason file_name.min.css.gz is not working, so solution with file_name.min.cssgz is working great. I hope that this will help someone.