Conflict when using server-side includes and mod_deflate compression - apache

I'm trying to enable site-wide compression using the apache mod_deflate module. This is on a shared server so whilst I have access to the .htaccess files, I cannot alter the server settings. The server has Apache 2.4.6 installed with mod_deflate
My problem is that by adding the requisite code to my .htaccess, my server side includes are disabled (possibly removed during compression?), or the whole thing just breaks and the site wont load.
My .htaccess code for enabling SSI is
AddHandler server-parsed .html
I use the following format for implementing SSI's in the HTML
<!--#include virtual="/includes/header.html" -->
The code I am supposed to add to the .htaccess for compression is:
<IfModule filter_module>
FilterDeclare COMPRESS
FilterProvider COMPRESS DEFLATE "%{CONTENT_TYPE} = 'text/html'"
FilterProvider COMPRESS DEFLATE "%{CONTENT_TYPE} = 'text/css'"
FilterProvider COMPRESS DEFLATE "%{CONTENT_TYPE} = 'text/plain'"
FilterProvider COMPRESS DEFLATE "%{CONTENT_TYPE} = 'text/xml'"
FilterProvider COMPRESS DEFLATE "%{CONTENT_TYPE} = 'text/x-component'"
FilterProvider COMPRESS DEFLATE "%{CONTENT_TYPE} = 'application/javascript'"
FilterProvider COMPRESS DEFLATE "%{CONTENT_TYPE} = 'application/json'"
FilterProvider COMPRESS DEFLATE "%{CONTENT_TYPE} = 'application/xml'"
FilterProvider COMPRESS DEFLATE "%{CONTENT_TYPE} = 'application/xhtml+xml'"
FilterProvider COMPRESS DEFLATE "%{CONTENT_TYPE} = 'application/rss+xml'"
FilterProvider COMPRESS DEFLATE "%{CONTENT_TYPE} = 'application/atom+xml'"
FilterProvider COMPRESS DEFLATE "%{CONTENT_TYPE} = 'application/vnd.ms-fontobject'"
FilterProvider COMPRESS DEFLATE "%{CONTENT_TYPE} = 'image/svg+xml'"
FilterProvider COMPRESS DEFLATE "%{CONTENT_TYPE} = 'application/x-font-ttf'"
FilterProvider COMPRESS DEFLATE "%{CONTENT_TYPE} = 'font/opentype'"
FilterProvider COMPRESS DEFLATE "%{CONTENT_TYPE} = 'image/x-icon'"
FilterProvider COMPRESS DEFLATE "%{CONTENT_TYPE} = '/application/(javascript|json|xml|x-javascript)/'"
FilterProvider COMPRESS DEFLATE "%{CONTENT_TYPE} = '/text/(html|css|javascript|plain|x(ml|-component))/'"
FilterChain COMPRESS
FilterProtocol COMPRESS DEFLATE change=yes;byteranges=no
</IfModule>
Adding the above causes an entire crash, removing the SSI's allows compression to work, but breaks the includes!

I had the same bug, i downgraded to Apache 2.2.22 and it worked.

Related

"The connection was reset" error caused by specific .htaccess rule

I've recently upgrade my local development environment to Apache 2.4.33 and PHP 7.1.7
No such issue using previous versions of Apache and PHP.
I have a css file which is called directly from the browser /css/styles.min.css It was working fine until I updated the file contents and saved it but now in both Firefox and Chrome I get the equivalent of "The connection was reset" errors.
File permissions and ownership are fine. No issues there.
I can fix it temporarily. I've narrowed it down to a single line in the .htaccess file
RewriteRule (.*)\.1[0-9]+\.(jpg|jpeg|gif|css|js) $1.$2
The line is in use, but doesn't match the file seeing the error. Commenting out that line resolves the issue in both Firefox and Chrome.
There must be an error log somewhere which will suggest a solution, but I can't find it in error_log or access_log
Really odd one. Hopefully somebody can suggest something I can try?
Update
If I only leave the minimum content in the .htaccess file, the problem no longer happens, even though the line is active.
Looks like it's mod_deflate related
I have no idea why removing a single line of the .htaccess file would temporarily resolve the issue, but the update below solves this issue with the css file and another issue with another url parsed by php which was giving the same error. So I'm now able to put .htaccess redirects back in how they were previously.
I replaced:
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/javascript application/x-javascript application/javascript text/x-component
AddOutputFilterByType DEFLATE text/richtext text/plain
AddOutputFilterByType DEFLATE image/svg+xml text/xsd text/xsl text/xml image/x-icon
AddOutputFilterByType DEFLATE application/json
</IfModule>
With an updated version which will function given no module, apache >= 2.4 or < 2.4
<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>
# HTML, TXT, CSS, JavaScript, JSON, XML, HTC:
<IfModule filter_module.c>
<IfModule version.c>
<IfVersion >= 2.4>
FilterDeclare COMPRESS
FilterProvider COMPRESS DEFLATE "%{CONTENT_TYPE} =~ m#^text/(html|css|plain|xml|x-component)#i"
FilterProvider COMPRESS DEFLATE "%{CONTENT_TYPE} =~ m#^application/(javascript|json|xml|xhtml+xml|rss+xml|atom+xml|vnd.ms-fontobject|x-font-ttf)#i"
FilterProvider COMPRESS DEFLATE "%{CONTENT_TYPE} =~ m#^image/(svg+xml|x-icon)#i"
FilterProvider COMPRESS DEFLATE "%{CONTENT_TYPE} = 'font/opentype'"
FilterChain COMPRESS
FilterProtocol COMPRESS DEFLATE change=yes;byteranges=no
</IfVersion>
<IfVersion < 2.4>
FilterDeclare COMPRESS
FilterProvider COMPRESS DEFLATE resp=Content-Type $text/html
FilterProvider COMPRESS DEFLATE resp=Content-Type $text/css
FilterProvider COMPRESS DEFLATE resp=Content-Type $text/plain
FilterProvider COMPRESS DEFLATE resp=Content-Type $text/xml
FilterProvider COMPRESS DEFLATE resp=Content-Type $text/x-component
FilterProvider COMPRESS DEFLATE resp=Content-Type $application/javascript
FilterProvider COMPRESS DEFLATE resp=Content-Type $application/json
FilterProvider COMPRESS DEFLATE resp=Content-Type $application/xml
FilterProvider COMPRESS DEFLATE resp=Content-Type $application/xhtml+xml
FilterProvider COMPRESS DEFLATE resp=Content-Type $application/rss+xml
FilterProvider COMPRESS DEFLATE resp=Content-Type $application/atom+xml
FilterProvider COMPRESS DEFLATE resp=Content-Type $application/vnd.ms-fontobject
FilterProvider COMPRESS DEFLATE resp=Content-Type $image/svg+xml
FilterProvider COMPRESS DEFLATE resp=Content-Type $image/x-icon
FilterProvider COMPRESS DEFLATE resp=Content-Type $application/x-font-ttf
FilterProvider COMPRESS DEFLATE resp=Content-Type $font/opentype
FilterChain COMPRESS
FilterProtocol COMPRESS DEFLATE change=yes;byteranges=no
</IfVersion>
</IfModule>
<IfModule !version.c>
FilterDeclare COMPRESS
FilterProvider COMPRESS DEFLATE resp=Content-Type $text/html
FilterProvider COMPRESS DEFLATE resp=Content-Type $text/css
FilterProvider COMPRESS DEFLATE resp=Content-Type $text/plain
FilterProvider COMPRESS DEFLATE resp=Content-Type $text/xml
FilterProvider COMPRESS DEFLATE resp=Content-Type $text/x-component
FilterProvider COMPRESS DEFLATE resp=Content-Type $application/javascript
FilterProvider COMPRESS DEFLATE resp=Content-Type $application/json
FilterProvider COMPRESS DEFLATE resp=Content-Type $application/xml
FilterProvider COMPRESS DEFLATE resp=Content-Type $application/xhtml+xml
FilterProvider COMPRESS DEFLATE resp=Content-Type $application/rss+xml
FilterProvider COMPRESS DEFLATE resp=Content-Type $application/atom+xml
FilterProvider COMPRESS DEFLATE resp=Content-Type $application/vnd.ms-fontobject
FilterProvider COMPRESS DEFLATE resp=Content-Type $image/svg+xml
FilterProvider COMPRESS DEFLATE resp=Content-Type $image/x-icon
FilterProvider COMPRESS DEFLATE resp=Content-Type $application/x-font-ttf
FilterProvider COMPRESS DEFLATE resp=Content-Type $font/opentype
FilterChain COMPRESS
FilterProtocol COMPRESS DEFLATE change=yes;byteranges=no
</IfModule>
</IfModule>
</IfModule>

IfModule filter_module Internal Server Error Apache 2.2 to 2.4

The below code in .htaccess is causing 500 Internal Server Error.
I have tried updating the code to Apache 2.4 but seems like I did a mistake somewhere.
Please advise whats wrong with the below code that is causing this Error:
<IfModule filter_module>
FilterDeclare COMPRESS
FilterProvider COMPRESS DEFLATE "%{Content-Type} = text/(html|css|javascript|plain|x(ml|-component))"
FilterProvider COMPRESS DEFLATE "%{Content-Type} = application/(javascript|json|xml|x-javascript)"
FilterChain COMPRESS
FilterProtocol COMPRESS change=yes;byteranges=no
</IfModule>
Tried this code as well and it didn't work:
<IfVersion >= 2.4>
<IfModule filter_module>
FilterDeclare COMPRESS
FilterProvider COMPRESS DEFLATE "%{Content-Type} =~ 'text/(html|css|javascript|plain|x(ml|-component))'"
FilterProvider COMPRESS DEFLATE "%{Content-Type} =~ 'application/(javascript|json|xml|x-javascript)'"
FilterChain COMPRESS
FilterProtocol COMPRESS change=yes;byteranges=no
</IfModule>
</IfVersion>
<IfVersion <= 2.2>
<IfModule filter_module>
FilterDeclare COMPRESS
FilterProvider COMPRESS DEFLATE resp=Content-Type /text/(html|css|javascript|plain|x(ml|-component))/
FilterProvider COMPRESS DEFLATE resp=Content-Type /application/(javascript|json|xml|x-javascript)/
FilterChain COMPRESS
FilterProtocol COMPRESS change=yes;byteranges=no
</IfModule>
</IfVersion>
After testing these two lines are the ones causing the 500 Internal Server Error:
FilterProvider COMPRESS DEFLATE "%{Content-Type} =~ 'text/(html|css|javascript|plain|x(ml|-component))'"
FilterProvider COMPRESS DEFLATE "%{Content-Type} =~ 'application/(javascript|json|xml|x-javascript)'"
Any help would be appreciated!!
Thank you. :)
For some reason apache 2.4 wasn't supporting the more compact code!
Anyway for documentation and if anyone face the same problem here is the WORKING Solution: (just mimic the changes)
<IfVersion >= 2.4>
<IfModule filter_module>
FilterDeclare COMPRESS
FilterProvider COMPRESS DEFLATE "%{CONTENT_TYPE} =~ m|^text/html|"
FilterProvider COMPRESS DEFLATE "%{CONTENT_TYPE} =~ m|^text/css|"
FilterProvider COMPRESS DEFLATE "%{CONTENT_TYPE} =~ m|^text/javascript|"
FilterProvider COMPRESS DEFLATE "%{CONTENT_TYPE} =~ m|^text/plain|"
FilterProvider COMPRESS DEFLATE "%{CONTENT_TYPE} =~ m|^text/xml|"
FilterProvider COMPRESS DEFLATE "%{CONTENT_TYPE} =~ m|^text/x-component|"
FilterProvider COMPRESS DEFLATE "%{CONTENT_TYPE} =~ m|^application/javascript|"
FilterProvider COMPRESS DEFLATE "%{CONTENT_TYPE} =~ m|^application/json|"
FilterProvider COMPRESS DEFLATE "%{CONTENT_TYPE} =~ m|^application/xml|"
FilterProvider COMPRESS DEFLATE "%{CONTENT_TYPE} =~ m|^application/x-javascript|"
FilterChain COMPRESS
FilterProtocol COMPRESS change=yes;byteranges=no
</IfModule>
</IfVersion>
<IfVersion <= 2.2>
<IfModule filter_module>
FilterDeclare COMPRESS
FilterProvider COMPRESS DEFLATE resp=Content-Type /text/(html|css|javascript|plain|xml|x-component)/
FilterProvider COMPRESS DEFLATE resp=Content-Type /application/(javascript|json|xml|x-javascript)/
FilterChain COMPRESS
FilterProtocol COMPRESS change=yes;byteranges=no
</IfModule>
</IfVersion>

HTTP Compression Not 100% on Apache (at Dreamhost)

I am trying to get gzip compression working on the site lavenderdiamond.net/home. I've added the following code to my .htaccess but none of the files that are served on my site are being compressed. I've also checked to see if mod_deflate is enabled on the server and it is. Here's the code:
# ----------------------------------------------------------------------
# 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>
I have a support ticket open with Dreamhost in case the problem is on their side, but I wonder if other people have had trouble getting compression enabled in general, or on Dreamhost specifically. Also I am very inexperienced in knowing how to debug possible problems with Apache. Is there a way to see error messages here and to know what might be going wrong that way? Any help would be well appreciated!
I was having this problem as well. I checked an older website I have hosted with Dreamhost and noticed the files were compressing correctly so I copied the relevant (older) H5P .htaccess chunk into my newer website and now it works.
Replace the "IfModule mod_filter.c" chunk with the following:
# HTML, TXT, CSS, JavaScript, JSON, XML, HTC:
<IfModule filter_module>
FilterDeclare COMPRESS
FilterProvider COMPRESS DEFLATE resp=Content-Type $text/html
FilterProvider COMPRESS DEFLATE resp=Content-Type $text/css
FilterProvider COMPRESS DEFLATE resp=Content-Type $text/plain
FilterProvider COMPRESS DEFLATE resp=Content-Type $text/xml
FilterProvider COMPRESS DEFLATE resp=Content-Type $text/x-component
FilterProvider COMPRESS DEFLATE resp=Content-Type $application/javascript
FilterProvider COMPRESS DEFLATE resp=Content-Type $application/json
FilterProvider COMPRESS DEFLATE resp=Content-Type $application/xml
FilterProvider COMPRESS DEFLATE resp=Content-Type $application/xhtml+xml
FilterProvider COMPRESS DEFLATE resp=Content-Type $application/rss+xml
FilterProvider COMPRESS DEFLATE resp=Content-Type $application/atom+xml
FilterProvider COMPRESS DEFLATE resp=Content-Type $application/vnd.ms-fontobject
FilterProvider COMPRESS DEFLATE resp=Content-Type $image/svg+xml
FilterProvider COMPRESS DEFLATE resp=Content-Type $image/x-icon
FilterProvider COMPRESS DEFLATE resp=Content-Type $application/x-font-ttf
FilterProvider COMPRESS DEFLATE resp=Content-Type $font/opentype
FilterChain COMPRESS
FilterProtocol COMPRESS DEFLATE change=yes;byteranges=no
</IfModule>
<IfModule !mod_filter.c>
# Legacy versions of Apache
AddOutputFilterByType DEFLATE text/html text/plain text/css application/json
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE text/xml application/xml text/x-component
AddOutputFilterByType DEFLATE application/xhtml+xml application/rss+xml application/atom+xml
AddOutputFilterByType DEFLATE image/x-icon image/svg+xml application/vnd.ms-fontobject application/x-font-ttf font/opentype
</IfModule>

videos not playing in Flowplayer after configuring mod deflate

I configured Apache/2.2.17 server on Windows 7 with mod deflate configured in .htaccess as
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$ no-gzip dont-vary
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
But everything worked fine except videos were not playing in Flowplayer so I changed (added SWF in exclude from gzip compression ) in .htaccess to
SetOutputFilter DEFLATE
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ \
no-gzip dont-vary
SetEnvIfNoCase Request_URI \
\.(?:exe|t?gz|zip|bz2|sit|rar|swf)$ \
no-gzip dont-vary
SetEnvIfNoCase Request_URI \.pdf$ no-gzip dont-vary
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
Now the video plays in flowplayer in all browsers except IE.
I want correct configuration to work in IE also.
The following config worked.
# 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>
# HTML, TXT, CSS, JavaScript, JSON, XML, HTC:
<IfModule filter_module>
FilterDeclare COMPRESS
FilterProvider COMPRESS DEFLATE resp=Content-Type $text/html
FilterProvider COMPRESS DEFLATE resp=Content-Type $text/css
FilterProvider COMPRESS DEFLATE resp=Content-Type $text/plain
FilterProvider COMPRESS DEFLATE resp=Content-Type $text/xml
FilterProvider COMPRESS DEFLATE resp=Content-Type $text/x-component
FilterProvider COMPRESS DEFLATE resp=Content-Type $application/javascript
FilterProvider COMPRESS DEFLATE resp=Content-Type $application/json
FilterProvider COMPRESS DEFLATE resp=Content-Type $application/xml
FilterProvider COMPRESS DEFLATE resp=Content-Type $application/xhtml+xml
FilterProvider COMPRESS DEFLATE resp=Content-Type $application/rss+xml
FilterProvider COMPRESS DEFLATE resp=Content-Type $application/atom+xml
FilterProvider COMPRESS DEFLATE resp=Content-Type $application/vnd.ms-fontobject
FilterProvider COMPRESS DEFLATE resp=Content-Type $image/svg+xml
FilterProvider COMPRESS DEFLATE resp=Content-Type $application/x-font-ttf
FilterProvider COMPRESS DEFLATE resp=Content-Type $font/opentype
FilterChain COMPRESS
FilterProtocol COMPRESS DEFLATE change=yes;byteranges=no
</IfModule>
<IfModule !mod_filter.c>
# Legacy versions of Apache
AddOutputFilterByType DEFLATE text/html text/plain text/css application/json
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE text/xml application/xml text/x-component
AddOutputFilterByType DEFLATE application/xhtml+xml application/rss+xml application/atom+xml
AddOutputFilterByType DEFLATE image/svg+xml application/vnd.ms-fontobject application/x-font-ttf font/opentype
</IfModule>
</IfModule>

Apache AddOutputFilterByType is deprecated. How to rewrite using mod_filter?

AddOutputFilterByType is deprecated in Apache.
The documentation says that the same functionality is available using mod_filter.
I currently use
AddOutputFilterByType DEFLATE text/html
What is the equivalent using mod_filter?
AddOutputFilterByType had severe limitations in httpd-2.2 so it was marked deprecated there. But in httpd-2.4 this directive was moved to filter_module, corrected and un-deprecated.
In apache 2.2 you should instead enable filter_module and deflate_module and use:
# Declare a "gzip" filter, it should run after all internal filters like PHP or SSI
FilterDeclare gzip CONTENT_SET
# "gzip" filter can change "Content-Length", can not be used with range requests
FilterProtocol gzip change=yes;byteranges=no
# Enable "gzip" filter if "Content-Type" contains "text/html", "text/css" etc.
FilterProvider gzip DEFLATE resp=Content-Type $text/html
FilterProvider gzip DEFLATE resp=Content-Type $text/css
FilterProvider gzip DEFLATE resp=Content-Type $text/javascript
FilterProvider gzip DEFLATE resp=Content-Type $application/javascript
FilterProvider gzip DEFLATE resp=Content-Type $application/x-javascript
# Add "gzip" filter to the chain of filters
FilterChain gzip
deflate_module would only serve compressed content to browsers that declare support for gzip encoding in request header.
I'm using mod_filter for substituing instead of deflating, but the idea is the same.
This is what worked for me (I'm doing reverse proxy and rewriting urls and links):
LoadModule substitute_module modules/mod_substitute.so
LoadModule filter_module modules/mod_filter.so
FilterDeclare MYFILTER
# syntax changed in Apache2.4 (see also https://httpd.apache.org/docs/current/mod/mod_filter.html)
FilterProvider MYFILTER SUBSTITUTE "%{Content_Type} = 'text/html'"
FilterProvider MYFILTER SUBSTITUTE "%{Content_Type} = 'text/xml'"
FilterProvider MYFILTER SUBSTITUTE "%{Content_Type} = 'application/javascript'"
FilterProvider MYFILTER SUBSTITUTE "%{Content_Type} = 'application/json'"
<Location /sial/>
ProxyPass http://localhost:8300/
ProxyPassReverse http://localhost:8300/
ProxyPassReverseCookiePath / /sial/
FilterChain MYFILTER
Substitute "s|/tea_sial_v2|/sial/tea_sial_v2|inq"
</Location>
It become un-deprecated in Apache 2.3.7, because it was moved/integrated into mod_filter. So, what I've got in:
Instead of:
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/css
using:
<IfModule mod_filter.c>
...