Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 810
Frodik Avatar asked Jan 31 '12 08:01

Frodik


People also ask

How do I gzip a javascript file?

htaccess file. Then load an html or js file via the server and check the headers for "Content-Encoding", if it says gzip or deflate, it is enabled. You need access to your vhost/server config to globally enable compression. You don't need to prepare your files, they're compressed automatically on request.

How do I open a compressed GZ file?

Select all the files and folders inside the compressed file, or multi-select only the files or folders you want to open by holding the CTRL key and left-clicking on them. Click 1-click Unzip, and choose Unzip to PC or Cloud in the WinZip toolbar under the Unzip/Share tab.

Are GZ files compressed?

A GZ file is an archive file compressed by the standard GNU zip (gzip) compression algorithm. It typically contains a single compressed file but may also store multiple compressed files. gzip is primarily used on Unix operating systems for file compression.


3 Answers

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.

like image 110
regilero Avatar answered Oct 10 '22 08:10

regilero


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
like image 36
tsilvestre Avatar answered Oct 10 '22 08:10

tsilvestre


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.

like image 9
Kevinoid Avatar answered Oct 10 '22 09:10

Kevinoid