How to force Apache to use manually pre-compressed gz file of CSS and JS files?

enable gzip compression apache xampp
apache deflate not working
gzip javascript
apache deflate vs gzip
accept-encoding: gzip
gzip json
check gzip compression chrome
disable http compression apache

I have simple question. I have webdirectory /css and inside is file style.css. I have manually gzipped this file and saved it as style.css.gz. I want to save CPU cycles to not have CSS file compressed at each request. How do I configure Apache to look for this .gz file and serve it instead of compressing .css file over and over again ?

Note: I don't want Apache to create .gz file itself. In my scenario I have to create .css.gz file manually - using PHP on specific requests.

Some RewriteRule should handle that quite well.

In a Drupal configuration file I found:

# AddEncoding allows you to have certain browsers uncompress information on the fly.
AddEncoding gzip .gz

#Serve gzip compressed CSS files if they exist and the client accepts gzip.
RewriteCond %{HTTP:Accept-encoding} gzip
RewriteCond %{REQUEST_FILENAME}\.gz -s
RewriteRule ^(.*)\.css $1\.css\.gz [QSA]

# Serve gzip compressed JS files if they exist and the client accepts gzip.
RewriteCond %{HTTP:Accept-encoding} gzip
RewriteCond %{REQUEST_FILENAME}\.gz -s
RewriteRule ^(.*)\.js $1\.js\.gz [QSA]

# Serve correct content types, and prevent mod_deflate double gzip.
RewriteRule \.css\.gz$ - [T=text/css,E=no-gzip:1]
RewriteRule \.js\.gz$ - [T=text/javascript,E=no-gzip:1]

This will make the job. You can put that either in a <Directory foo/> section or in a .htaccess file if you do not have access to apache configuration or if you want to slowdown your webserver.

How can I gzip my JavaScript and CSS files?, Every time you request a CSS, JavaScript, HTML, … file, the In Apache, there's a workaround you can use where you actually pre-compress the <IfModule mod_headers.c> # Serve gzip compressed CSS files if they exist gzip # Force proxies to cache gzipped & # non-gzipped css/js files separately. Pre-compression with gzip and brotli in Apache. My blog is generated using Hugo, as I explained before, which is a static site generator.Since all the pages are generated beforehand, their compressed version can be too, so the webserver will only have to send the compressed file instead of compressing on the fly each time a page is called.

Continuing on regilero answer you should also add a few more lines in order to make sure the server answers with the corresponding Content-Encoding header:

#Serve gzip compressed CSS files if they exist and the client accepts gzip.
RewriteCond %{HTTP:Accept-encoding} gzip
RewriteCond %{REQUEST_FILENAME}\.gz -s
RewriteRule ^(.*)\.css $1\.css\.gz [QSA]

# Serve gzip compressed JS files if they exist and the client accepts gzip.
RewriteCond %{HTTP:Accept-encoding} gzip
RewriteCond %{REQUEST_FILENAME}\.gz -s
RewriteRule ^(.*)\.js $1\.js\.gz [QSA]

# Serve correct content types, and prevent mod_deflate double gzip.
RewriteRule \.css\.gz$ - [T=text/css,E=no-gzip:1,E=is_gzip:1]
RewriteRule \.js\.gz$ - [T=text/javascript,E=no-gzip:1,E=is_gzip:1]
Header set Content-Encoding "gzip" env=is_gzip

Pre-compress content for Apache to avoid compressing on every , compress text, html, javascript, css, xml: AddOutputFilterByType DEFLATE If you can't change your .htaccess file, you can use PHP to return compressed content Feel free to download the files, put them on your server and tweak the settings Apache mod_deflate has some rules to avoid compression for older browsers  by using Files clause (we could add this globally in httpd.conf) <files *.css.gz> ForceType text/css Header set Content-Encoding "gzip" </files> <files *.js.gz> #ForceType text/javascript # lately this one is more popular ForceType application/javascript Header set Content-Encoding "gzip" </files> Both ways work fine.

Rather than implementing content negotiation yourself using mod_rewrite (and missing out on more advanced features like q-values, status 406, TCN, etc.) you may want to use mod_negotiation as discussed in this question. Copying my answer from there:

Options +MultiViews
RemoveType .gz
AddEncoding gzip .gz
<FilesMatch ".+\.tar\.gz$">
    RemoveEncoding .gz
    # Note:  Can use application/x-gzip for backwards-compatibility
    AddType application/gzip .gz
</FilesMatch>

This has the added bonus of working for all .gz files rather than just .css.gz and .js.gz and being easily extended for additional encodings.

It does have one major drawback, since only requests for files which do not exist are negotiated a file named foo.js would make requests for /foo.js (but not /foo) return the uncompressed version. This can be avoided using François Marier's solution of renaming uncompressed files with a double extension, so foo.js is deployed as foo.js.js. If this restriction isn't too painful for your deployment process, using mod_negotiation may be worth considering.

mod_deflate, Apache – precompressing static files with gzip | /9076752/how-to-force-apache​-to-use-manually-pre-compressed-gz-file-of-css-and-js-files. Actually, this works insofar as the .gz file is sent instead of the .css, if the request goes to /static/style.css. The problem is only, that the file is delivered as "application/x-gzip" and not as "text/css". Using mod_rewrite's T flag doesn't alter this. Neither does an explicit. AddType text/css .css

I realise this isn't what you asked, but I'm throwing it out there anyway. Have you considered simply using mod_cache with mod_gzip? With the right configuration apache will gzip the files for you then pass the compressed cache files to clients and downstream proxies. This is way more efficient than what you propose in terms of time and effort and equally efficient in terms of bandwidth and computational load.

It doesn't matter that PHP has compressed the files first because with the right settings mod_gzip will detect that. The main point is you can cache compressed files and Apache won't recompress them.

How To Optimize Your Site With GZIP Compression – BetterExplained, Given that I was already going to minify my Javascript and CSS files ahead of After all, it starts as readable source code and ends up as an unreadable binary file. and compress all CSS and Javascript files using YUI Compressor and gzip : gzip gz ForceType text/css Options +Multiviews SetEnv force-no-vary Header​  If the client cannot handle compressed content the server will find the the .gz file, decompress it, change the headers accordingly and then serve the file. I'm asking your help as cannot find a way to evaluate the if block using the Apache rules.

I was struggling slightly with the proposed solution. For anyone else using VirtualHosts with different DocumentRoots you simply have to change the RewriteCond to:

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME}\.gz -s

Apache – precompressing static files with gzip – timor's site, Using Apache 2.2 and 2.4 to serve pre-compressed gzip content instead of Instead of gzipping html, js and css on each and every request, create gzipped files and serve them then serve the webp-file instead of the requested jpeg. Force compression for mangled `Accept-Encoding` request headers  31 How to force Apache to use manually pre-compressed gz file of CSS and JS files? 31 Is there any compatible memory cache for PHP 7? View more network posts →

Serving pre-compressed files using Apache, Reduce the size of files sent from your server to increase the speed to which they Gzip/Brotli locates similar strings within a text file and replaces those strings so well in a web environment is because CSS files and HTML files use a lot of compression depending on whether or not you've got an IIS or Apache server (or​  It is possible to serve pre-compressed files using mod_negotiation although it is a bit finicky. The primary difficulty is that only requests for files which do not exist are negotiated. So if foo.js and foo.js.gz both exist, responses for /foo.js will always be uncompressed (although responses for /foo would work correctly).

Apache + mod_deflate: serve pre-compressed content instead of , I can use relayd to forward the requests to httpd on two different ports, based on -to-force-apache-to-use-manually-pre-compressed-gz-file-of-css-and-js-files. This leaves the original files intact and adds minified .css.css and .js.js files as well as minified and compressed .css.gz and .js.gz files. How browsers advertise gzip support The nice thing about serving compressed content to browsers is that browsers that support receiving gzipped content (almost all of them nowadays) include the following

Enable compression, 15 thoughts on “ How to compress .php, .css and .js files without mod_gzip or mod_deflate ” Pavel 27 July 2010 at 2:36 am. I was trying to implement the solution for gzipping css and js files on godaddy, and it worked (gzipped file was served instead of normal), except for one problem: Firefox / chrome could not decompress the files outputting half-decompressed contents, so js stopped

Comments
  • I do something with PHP. On my CDN, Apache rewrites all js and css files requests to a php script which serves the compressed file if accepted by the browser or the minified file otherwise. It compares first the last modification time of both files (original & compressed), and only generates the compressed file if needed. If you use PHP and are interested I can paste it.
  • LOL - good comment about htaccess and slowing down webserver ;-)
  • I've set up your solution, however waiting times for delivering resources (JS+CSS) are still high 50 - 100 ms in average so I am supposing that Apache is not using these rules or something is wrong, because when serving static files the waiting time seen in Dev tools in Chrome should be smaller, right ?
  • check the request Accept-encoding is really gzip. The fact you may have several value in Accept-Encoding is maybe breaking it. Usually I use it with Varnish behind the server and this header is a simplier version of the variants: varnish-cache.org/trac/wiki/FAQ/… . try to match the Accept-Ending RewriteCond with the one you have (RewriteCond %{HTTP:Accept-encoding} gzip,deflate) to sse if it comes from that problem
  • Old answer but still very useful. In my case (1and1 hosting) I had to change the RewriteRules to RewriteRule ^(.*)\.css %{REQUEST_URI}\.gz [QSA] and RewriteRule ^(.*)\.js %{REQUEST_URI}\.gz [QSA]. Otherwise the server returned 404. Hope it helps somebody else.
  • Tried to do both .js and .json; ended up appending the $ end-of-string anchor on the Pattern side of RewriteRule: RewriteRule ^(.*)\.js$ $1\.js\.gz [QSA] ...otherwise the js rewrite caught the .json files and rewrote them to .js.gz rather than .json.gz.
  • Thanks for this addendum, I first tried using regilero's solution, but the files were returned without the Content-Encoding header and thus failed to work right.
  • httpd.apache.org/docs/2.4/mod/mod_deflate.html#precompressed describes something similar. They also mention Header append Vary Accept-Encoding, too.
  • Apache will automatically add headers used by RewriteCond to Vary: If a HTTP header is used in a condition this header is added to the Vary header of the response. httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritecond (see also "novary" flag)
  • Good catch @mems. You are quite right, I stand corrected. I've removed the incorrect statement about the Vary header not being set properly.
  • This is the answer that worked for me. Thanks! Great solution!