.htaccess rule for compressing files with custom extension - apache

I have the following .htaccess file copied directly from html5boilerplate.com:
<IfModule mod_deflate.c>
# Force deflate for mangled headers developer.yahoo.com/blogs/ydn/posts/2010/12/pushing-beyond-gzipping/
<IfModule mod_setenvif.c>
<IfModule mod_headers.c>
SetEnvIfNoCase ^(Accept-EncodXng|X-cept-Encoding|X{15}|~{15}|-{15})$ ^((gzip|deflate)\s*,?\s*)+|[X~-]{4,13}$ HAVE_Accept-Encoding
RequestHeader append Accept-Encoding "gzip,deflate" env=HAVE_Accept-Encoding
</IfModule>
</IfModule>
AddOutputFilterByType DEFLATE application/atom+xml \
application/javascript \
application/json \
application/rss+xml \
application/vnd.ms-fontobject \
application/x-font-ttf \
application/xhtml+xml \
application/xml \
font/opentype \
image/svg+xml \
image/x-icon \
text/css \
text/html \
text/plain \
text/x-component \
text/xml
</IfModule>
YSlow shows only one file that isn't compressed and it's filename is testing.cache and it's content is a mix of html and css. I renamed the file to testing.html and the file got compressed just fine. I expected that the testing.cache file will get compressed too since it falls into the text/html group (this is a file I'm loading via ajax on page load). So, am wondering if I can something like:
<FilesMatch "\.(cache)$">
someDirectiveToCache .cache file
</FilesMatch>
I've looked at mod_deflate for any matching directive but to no luck. Sure I could leave it as testing.html but am wondering how it could be done for testing.cache. Also, I presumed the FilesMatch can be used within the <IfModule mod_deflate.c> module, since it can be (tested and using it) used inside the <IfModule mod_expires.c> like this:
<FilesMatch "\.(cache)$">
ExpiresDefault "access plus 1 hour"
</FilesMatch>
My Apache version (if important) is: 2.2.15.

I managed to get this working, quite easy in the end actually. I went through all the documentation to find the AddOutputFilter directive which is plain simple and actually works with extensions.
The AddOutputFilter directive syntax as stated in the docs for version 2.2 is:
AddOutputFilter filter[;filter...] extension [extension] ...
In my example after the AddOutputFilterByType directive I added:
AddOutputFilter DEFLATE cache
Hope this helps someone in the future.

Related

Neither gzip nor deflate compression doesn't work for Apache server using mod_deflate or AddOutputFilterByType

I'm trying to compress SVG files on my server, but whatever code i put in .htaccess it doesn't work!
This is my .htaccess code:
# ----------------------------------------------------------------------
# Proper MIME type for all files
# ----------------------------------------------------------------------
# JavaScript
# Normalize to standard type (it's sniffed in IE anyways)
# tools.ietf.org/html/rfc4329#section-7.2
AddType application/javascript js jsonp
AddType application/json json
# Audio
AddType audio/ogg oga ogg
AddType audio/mp4 m4a f4a f4b
# Video
AddType video/ogg ogv
AddType video/mp4 mp4 m4v f4v f4p
AddType video/webm webm
AddType video/x-flv flv
# SVG
# Required for svg webfonts on iPad
# twitter.com/FontSquirrel/status/14855840545
AddType image/svg+xml svg svgz
AddEncoding gzip svgz
# Webfonts
AddType application/vnd.ms-fontobject eot
AddType application/x-font-ttf ttf ttc
AddType font/opentype otf
AddType application/x-font-woff woff
# Assorted types
AddType image/x-icon ico
AddType image/webp webp
AddType text/cache-manifest appcache manifest
AddType text/x-component htc
AddType application/xml rss atom xml rdf
AddType application/x-chrome-extension crx
AddType application/x-opera-extension oex
AddType application/x-xpinstall xpi
AddType application/octet-stream safariextz
AddType application/x-web-app-manifest+json webapp
AddType text/x-vcard vcf
AddType application/x-shockwave-flash swf
AddType text/vtt vtt
# ----------------------------------------------------------------------
# Gzip compression
# ----------------------------------------------------------------------
<Location />
# Insert filter
SetOutputFilter DEFLATE
# Netscape 4.x has some problems...
BrowserMatch ^Mozilla/4 gzip-only-text/html
# Netscape 4.06-4.08 have some more problems
BrowserMatch ^Mozilla/4\.0[678] no-gzip
# MSIE masquerades as Netscape, but it is fine
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
# Don't compress images
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary
# Make sure proxies don't deliver the wrong content
Header append Vary User-Agent env=!dont-vary
</Location>
# ----------------------------------------------------------------------
# Gzip compression
# ----------------------------------------------------------------------
<IfModule mod_deflate.c>
# Force deflate for mangled headers developer.yahoo.com/blogs/ydn/posts/2010/12/pushing-beyond-gzipping/
<IfModule mod_setenvif.c>
<IfModule mod_headers.c>
SetEnvIfNoCase ^(Accept-EncodXng|X-cept-Encoding|X{15}|~{15}|-{15})$ ^((gzip|deflate)\s*,?\s*)+|[X~-]{4,13}$ HAVE_Accept-Encoding
RequestHeader append Accept-Encoding "gzip,deflate" env=HAVE_Accept-Encoding
</IfModule>
</IfModule>
# Compress all output labeled with one of the following MIME-types
<IfModule mod_filter.c>
AddOutputFilterByType DEFLATE application/atom+xml \
application/javascript \
application/json \
application/rss+xml \
application/xhtml+xml \
application/xml \
application/vnd.ms-fontobject \
application/x-font-ttf \
font/opentype \
image/x-icon \
image/svg+xml \
text/css \
text/html \
text/plain \
text/x-component \
text/xml
</IfModule>
</IfModule>
When I look at the response headers of my SVG files, it appears there is no GZip compression.
It happens the same when I analyze the page via Google PageSpeed Insights.
Can anyone help me?
Use the deflate module, since the gzip module is not part of the standard Apache installation.
Add the following to use mod_deflate:
<filesMatch "\.(svg|gif|jpg|jpeg|png|ico|swf|js|css|html)$">
Header set Content-Encoding compress
# Header set Content-Encoding x-compress
</filesMatch>
mod_gzip is a standalone project.
When responding with an encoding Apache will use whatever form (i.e., x-foo or foo) the client requested. If the client didn't specifically request a particular form Apache will use the form given by the AddEncoding directive. To make this long story short, you should always use x-gzip and x-compress for these two specific encodings. More recent encodings, such as deflate, should be specified without the x-.
References
How to check if my shared hosting provider has mod_gzip installed?
mod_deflate
Header Directive
Content Negotiation
The Accept Encoding Header
Content-Encoding != Content-Type
mod_negotiation
mod_mime
Apache Compile How-to

How to get mod_deflate running on Heroku

Our app is PHP running on Apache. I'm trying to install mod_deflate, however I get the error:
httpd: Syntax error on line 433 of /app/apache/conf/httpd.conf: Syntax error on line 19 of /app/www/heroku/conf/default.conf: Cannot load /app/www/heroku/ext/mod_deflate.so into server: /lib/libc.so.6: version `GLIBC_2.14' not found (required by /app/www/heroku/ext/mod_deflate.so)
Does anyone know what I need to do to get this working?
This is my default.conf:
DocumentRoot "/app/www/public"
<Directory "/app/www/public">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
#
# Allow more concurrent connections per dyno
#
ServerLimit 12
MaxClients 12
#
# Enable compression with mod_deflate
#
LoadModule deflate_module /app/www/heroku/ext/mod_deflate.so
<IfModule mod_deflate.c>
# Set compression level
DeflateCompressionLevel 6
# Force compression for mangled headers.
# http://developer.yahoo.com/blogs/ydn/posts/2010/12/pushing-beyond-gzipping
<IfModule mod_setenvif.c>
<IfModule mod_headers.c>
SetEnvIfNoCase ^(Accept-EncodXng|X-cept-Encoding|X{15}|~{15}|-{15})$ ^((gzip|deflate)\s*,?\s*)+|[X~-]{4,13}$ HAVE_Accept-Encoding
RequestHeader append Accept-Encoding "gzip,deflate" env=HAVE_Accept-Encoding
</IfModule>
</IfModule>
# Compress all output labeled with one of the following MIME-types
AddOutputFilterByType DEFLATE application/atom+xml \
application/javascript \
application/json \
application/rss+xml \
application/vnd.ms-fontobject \
application/x-font-ttf \
application/x-web-app-manifest+json \
application/xhtml+xml \
application/xml \
font/opentype \
image/svg+xml \
image/x-icon \
text/css \
text/html \
text/plain \
text/x-component \
text/xml
</IfModule>

mod_deflate disabled: assets still being gzipped

When I execute:
sudo a2dismod deflate
sudo service apache2 restart
And refresh & inspect the response headers of my site in Chrome Web Tools, my assets (css and js) are still being marked as Content-Encoding : gzip. Y-Slow also confirms that all assets except the actual html document are still compressed.
I have output compression turned off in CodeIgniter's config file:
$config['compress_output'] = FALSE;
For what it's worth... though with the deflate module disabled I don't think it matters much, my /etc/apache2/mods-available/deflate.conf file is the system default:
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/x-javascript application/javascript application/ecmascript
AddOutputFilterByType DEFLATE application/rss+xml
</IfModule>
I'm running Ubuntu 12.04. What else could be causing my assets to be compressed?
My .htaccess file is void of deflate commands.
You can try the following.
to disable the gzip for a specified file try this:
SetEnvIfNoCase Request_URI YOUR_FILE_NAME\.php$ no-gzip dont-vary
in you htaccess file. change YOUR_FILE_NAME\.PHP$ to what you need.
and to disable the gzip at all try this:
SetEnv no-gzip dont-vary
in your htaccess file.

Apache mod_deflate: Curl VS everything else

I hope someone can help me with this as it's driving my crazy.
I have set up mod_deflate on a server, everything SEEMS to be set up correctly, however when I access a CSS or JavaScript file via a browser it's saying the files are not compressed.
However, When I do a curl request, the response headers are saying the file is being compressed.
YES, the module is installed and enabled as it is successfully logging:
When I request the file via a browser (Chrom, FF etc), wget, or any of the online page speed tests, it logs something like
"GET /var/cache/_2c95fa0249c997011610ad7ddd3332a8.css HTTP/1.0" gzip, deflate -/- (-%)
When I request via curl, it provides a log like
"HEAD /var/cache/_2c95fa0249c997011610ad7ddd3332a8.css HTTP/1.0" gzip, deflate 108466/358074 (30%)
Is there a way to check to see if mod_deflate is actually working? Am I missing something in the setup of mod_deflate? Could there be a conflict with the expires directive and mod_deflate (I have removed everything but the mod_deflate and it still didn't work)?
Here are the configurations I have set up:
main conf file
<IfModule mod_deflate.c>
DeflateCompressionLevel 4
DeflateFilterNote Input instream
DeflateFilterNote Output outstream
DeflateFilterNote Ratio ratio
LogFormat '"%r" %{Accept-Encoding}i %{outstream}n/%{instream}n (%{ratio}n%%)' deflate
CustomLog logs/deflate_log deflate
</IfModule>
.htaccess file
<FilesMatch "\.(css|js)$">
Allow from all
</FilesMatch>
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 1 seconds"
ExpiresByType text/css "access plus 604800 seconds"
ExpiresByType text/javascript "access plus 216000 seconds"
ExpiresByType application/x-javascript "access plus 216000 seconds"
</IfModule>
<IfModule mod_headers.c>
<FilesMatch "\\.(css)$">
Header set Cache-Control "max-age=2692000, public"
</FilesMatch>
<FilesMatch "\\.(js)$">
Header set Cache-Control "max-age=216000, private"
</FilesMatch>
Header unset ETag
Header unset Last-Modified
</IfModule>
<IfModule mod_deflate.c>
SetOutputFilter DEFLATE
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.(?:pdf|avi|mov|mp3|mp4|rm)$ no-gzip dont-vary
#Dealing with proxy servers
<IfModule mod_headers.c>
Header append Vary User-Agent env=!dont-vary
</IfModule>
</IfModule>
The file in question:
http://diva.akqire.com/var/cache/_2c95fa0249c997011610ad7ddd3332a8.css
Add the following header:
Header set Content-Encoding x-deflate
Apache servers don't send the Content-Length response header if gzip encoding is used.
Is there a way to check to see if mod_deflate is actually working?
Use a custom log to track deflate usage:
DeflateFilterNote Input input_info
DeflateFilterNote Output output_info
DeflateFilterNote Ratio ratio_info
LogFormat '"%r" %{output_info}n/%{input_info}n (%{ratio_info}n%%)' deflate
CustomLog /var/log/httpd/deflate_log deflate
References
Configuring mod_deflate
Mod Deflate - Compress Your Web Pages and Save Bandwidth
How To Save Traffic With Apache2's mod_deflate

How can I get Apache gzip compression to work?

I can't get my site to use gzip compression.
I recently watched this video by Chris Coyier over at css-tricks.com. In the video, he talks about enabling gzip compression to make websites run faster.
As per his instruction, I linked through to github via html5boilerplate.com, copied the gzip compression code from their .htaccess file, pasted it into my own, and uploaded it to my site.
I've tested it via gzipwtf.com and it doesn't seem to work. Can anyone help me with this?
My .htaccess file looks like this:
# ----------------------------------------------------------------------
# Trim www
# ----------------------------------------------------------------------
RewriteEngine On
RewriteCond %{HTTP_HOST} !^orbitprint.com$ [NC]
RewriteRule ^(.*)$ http://orbitprint.com/$1 [L,R=301]
# ----------------------------------------------------------------------
# Gzip compression
# ----------------------------------------------------------------------
<IfModule mod_deflate.c>
# Force deflate for mangled headers developer.yahoo.com/blogs/ydn/posts/2010/12/pushing-beyond-gzipping/
<IfModule mod_setenvif.c>
<IfModule mod_headers.c>
SetEnvIfNoCase ^(Accept-EncodXng|X-cept-Encoding|X{15}|~{15}|-{15})$ ^((gzip|deflate)\s*,?\s*)+|[X~-]{4,13}$ HAVE_Accept-Encoding
RequestHeader append Accept-Encoding "gzip,deflate" env=HAVE_Accept-Encoding
</IfModule>
</IfModule>
# Compress all output labeled with one of the following MIME-types
<IfModule mod_filter.c>
AddOutputFilterByType DEFLATE application/atom+xml \
application/javascript \
application/json \
application/rss+xml \
application/vnd.ms-fontobject \
application/x-font-ttf \
application/xhtml+xml \
application/xml \
font/opentype \
image/svg+xml \
image/x-icon \
text/css \
text/html \
text/plain \
text/x-component \
text/xml
</IfModule>
</IfModule>
Try this :
####################
# GZIP COMPRESSION #
####################
SetOutputFilter DEFLATE
AddOutputFilterByType DEFLATE text/html text/css text/plain text/xml application/x-javascript application/x-httpd-php
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip
It is better to implement it as in the snippet below.
Just paste the content below in your .htaccess file, then, check the performance using: Google PageSpeed, Pingdom Tools and GTmetrics.
# Enable GZIP
<ifmodule mod_deflate.c>
AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
</ifmodule>
# Expires Headers - 2678400s = 31 days
<ifmodule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 1 seconds"
ExpiresByType text/html "access plus 7200 seconds"
ExpiresByType image/gif "access plus 2678400 seconds"
ExpiresByType image/jpeg "access plus 2678400 seconds"
ExpiresByType image/png "access plus 2678400 seconds"
ExpiresByType text/css "access plus 518400 seconds"
ExpiresByType text/javascript "access plus 2678400 seconds"
ExpiresByType application/x-javascript "access plus 2678400 seconds"
</ifmodule>
# Cache Headers
<ifmodule mod_headers.c>
# Cache specified files for 31 days
<filesmatch "\.(ico|flv|jpg|jpeg|png|gif|css|swf)$">
Header set Cache-Control "max-age=2678400, public"
</filesmatch>
# Cache HTML files for a couple hours
<filesmatch "\.(html|htm)$">
Header set Cache-Control "max-age=7200, private, must-revalidate"
</filesmatch>
# Cache PDFs for a day
<filesmatch "\.(pdf)$">
Header set Cache-Control "max-age=86400, public"
</filesmatch>
# Cache Javascripts for 31 days
<filesmatch "\.(js)$">
Header set Cache-Control "max-age=2678400, private"
</filesmatch>
</ifmodule>
Your .htaccess should run just fine; it depends on four different Apache modules (one per each <IfModule> directive). I guess one of the following:
your Apache server doesn't have either mod_filter, mod_deflate, mod_headers and/or mod_setenvif modules installed and running. If you can access the server config, please check /etc/apache2/httpd.conf (and the related Apache config files); otherwise, you can see which modules are loaded via phpinfo(), under the apache2handler section (see attached image); (EDIT) OR, you can open a terminal window and issue the command sudo apachectl -M that will list the loaded modules;
if you get an http 500 internal server error, your server may not be allowed to use .htaccess files;
you are trying to load a PHP file that sends its own headers (overwriting Apache'sheaders), thus "confusing" the browser.
In any case, you should double-check your server config and error logs to see what's going wrong. Just to be sure, try to use the fastest way suggested here in Apache docs:
AddOutputFilterByType DEFLATE text/html text/plain text/xml
and then try to load a large textfile (preferably, clean your cache first).
(EDIT) If the needed modules are there (in the Apache modules dir) but aren't loaded,
just edit /etc/apache2/httpd.conf and add a LoadModule directive for each one of them.
If the needed modules aren't there (neither loaded, nor in the Apache modules directory), I fear that the only option is reinstalling Apache (a complete version).
First of all go to apache/bin/conf/httpd.conf and make sure that mod_deflate.so is enabled.
Then go to the .htaccess file and add this line:
SetOutputFilter DEFLATE
This should output all the content served as gzipped, i have tried it and it works.
Ran into this problem using the same .htaccess configuration. I realized that my server was serving javascript files as text/javascript instead of application/javascript. Once I added text/javascript to the AddOutputFilterByType declaration, gzip started working.
As to why javascript was being served as text/javascript: there was an AddType 'text/javascript' js declaration at the top of my root .htaccess file. After removing it (it had been added in error), javascript starting serving as application/javascript.
In my case i have used following code for enabling gzip compression in apache web server.
# Compress HTML File, CSS File, JavaScript File, Text File, XML File and Fonts
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE application/json
AddOutputFilterByType DEFLATE application/x-httpd-php
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE font/otf
AddOutputFilterByType DEFLATE font/ttf
I have taken reference from http://www.tutsway.com/enable-gzip-compression-using-htacess.php.
If your Web Host is through C Panel Enable G ZIP Compression on Apache C Panel
Go to CPanel and check for software tab.
Previously Optimize website used to work but now a new option is available i.e "MultiPHP INI Editor".
Select the domain name you want to compress.
Scroll down to bottom until you find zip output compression and enable it.
Now check again for the G ZIP Compression.
You can follow the video tutorial also. https://www.youtube.com/watch?v=o0UDmcpGlZI
In my case append only this line worked
SetOutputFilter DEFLATE
Enable compression via .htaccess
For most people reading this, compression is enabled by adding some code to a file called .htaccess on their web host/server. This means going to the file manager (or wherever you go to add or upload files) on your webhost.
The .htaccess file controls many important things for your site.
The code below should be added to your .htaccess file...
<ifModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>
Save the .htaccess file and then refresh your webpage.
Check to see if your compression is working using the Gzip compression tool.
<ifModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>
<IfModule mod_deflate.c>
# Insert filters
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/x-httpd-php
AddOutputFilterByType DEFLATE application/x-httpd-fastphp
AddOutputFilterByType DEFLATE image/svg+xml
# Drop problematic browsers
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
# Make sure proxies don't deliver the wrong content
Header append Vary User-Agent env=!dont-vary
</IfModule>