500 error when compressing files with .htaccess - php

I'm trying to compress files with .htaccess but I'm getting a 500 internal error,
This is the code I'm trying to use.
# JavaScript compression htaccess ruleset
AddHandler application/x-httpd-php .js
php_value auto_prepend_file gzip-js.php
php_flag zlib.output_compression On
I've also tried this but getting the same error,
<files *.html>
SetOutputFilter DEFLATE
</files>
Any suggestions why?

Do you have mod_deflate installed? Try this instead and see if your error goes away.
<ifModule mod_deflate.c>
<Files *.html>
SetOutputFilter DEFLATE
</Files>
</ifModule>
If this resolves the 500 error, then you probably don't have mod_deflate installed. You can test it by visiting HTTP Compression Test.
If you have access, you may be able to enable it by uncommenting the following lines in your httpd.conf file.
LoadModule filter_module modules/mod_filter.so
LoadModule deflate_module modules/mod_deflate.so
Then you will need to add an output type for each file type you wish to compress.
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
As mentioned in the comments, AddOutputFilterByType depends on mod_filter. If you still get a 500 error you should check that this module is loaded too.
You can read more here: Use mod_deflate to Compress Web Content delivered by Apache
I can't find any documentation that suggests whether Siteground supports mod_deflate or not. Their KB suggests using gZip from PHP.
How to enable gZIP compression for your pages?
How to compress my CSS with gZIP?

After spending hours of search I found this article, thought someone else might find it helpful,
Enjoy.

## DEFLATE ##
#mod_filter.so and mod_deflate.so
should be ON !!!!
<ifmodule mod_deflate.c>
AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript
</ifmodule>

This worked fine for me (paste in .htaccess):
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

Related

WordPress compression not working on localhost, XAMPP

I need some help with WordPress speed optimization via .htaccess.
For now, the website is still running on localhost, XAMPP, Apache v3.2.4.
What I did / tried:
turn on GZip in .htaccess
enable mod deflate in .htacess
place .htaccess in WordPress root directory
Google Lighthouse Speed Test via the Chrome dev tools does not detect:
Compression (message: Enable text compression)
When I examine the response header via dev tools, there's no cache-control: ... entry.
I would appreciate some help on how to fix these problems. I figure that I went wrong somewhere in my setup, but I'm not sure where.
So, here's what I did for COMPRESSION in .htacces:
<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>
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE image/x-icon
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/x-font
AddOutputFilterByType DEFLATE application/x-font-truetype
AddOutputFilterByType DEFLATE application/x-font-ttf
AddOutputFilterByType DEFLATE application/x-font-otf
AddOutputFilterByType DEFLATE application/x-font-opentype
AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
AddOutputFilterByType DEFLATE font/ttf
AddOutputFilterByType DEFLATE font/otf
AddOutputFilterByType DEFLATE font/opentype
# For Olders Browsers Which Can't Handle Compression
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
</IfModule>
<IfModule mod_headers.c>
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{REQUEST_FILENAME}\.gz -f
RewriteRule \.(css|ics|js|json|html|svg)$ %{REQUEST_URI}.gz [L]
# Prevent mod_deflate double gzip
RewriteRule \.gz$ - [E=no-gzip:1]
<FilesMatch "\.gz$">
# Serve correct content types
<IfModule mod_mime.c>
# (1)
RemoveType gz
# Serve correct content types
AddType text/css css.gz
AddType text/calendar ics.gz
AddType text/javascript js.gz
AddType application/json json.gz
AddType text/html html.gz
AddType image/svg+xml svg.gz
# Serve correct content charset
AddCharset utf-8 .css.gz \
.ics.gz \
.js.gz \
.json.gz
</IfModule>
# Force proxies to cache gzipped and non-gzipped files separately
Header append Vary Accept-Encoding
</FilesMatch>
# Serve correct encoding type
AddEncoding gzip .gz
</IfModule>

Optimization of magento website

I am using magento 1.8.1 and it is very slow. I want to optimize this, for that I am using some free module for HTML, CSS and JS minification but its doesn't work.After using that it create lots of error. I've referred website such like after merge JS, the custom module in front-end is stop working. I've checked lots of tutorial also for gzip compression and enabling Mod_deflect, I've followed all the step like put some code in .htaccess page like
<IfModule mod_deflate.c>
# Compress HTML, CSS, JavaScript, Text, XML and fonts
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
AddOutputFilterByType DEFLATE application/x-font
AddOutputFilterByType DEFLATE application/x-font-opentype
AddOutputFilterByType DEFLATE application/x-font-otf
AddOutputFilterByType DEFLATE application/x-font-truetype
AddOutputFilterByType DEFLATE application/x-font-ttf
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE font/opentype
AddOutputFilterByType DEFLATE font/otf
AddOutputFilterByType DEFLATE font/ttf
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE image/x-icon
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/xml
# Remove browser bugs (only needed for really old browsers)
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
Header append Vary User-Agent
</IfModule>
But that also didn't work.
this is my website: Link
Please help me into this.
Magento has the option to merge css and js files which works for me. I minimise the css and js files I use but I DON'T minimise the standard Magento files (epsecially prototype. I've read that this can break things).
As for compression, it's possible that you can't use the standard htaccess approach. See Compressing js & css files where mod deflate not available for a way around it.
Try to merge CSS and JS byon the admin panel System > Configuration > Developer (Be careful with the JS, the site stop working right in my case).
The thing that change everything for me it's changing the cache module. Magento use a cache module witch use the Zend_Cache library and create a huge numbers of files that makes the site terrible slow. You can easy replace it with this:
Cm_Cache_Backend_File
or even better, magento is prepeared to work with Redis. With this feature the cache goes to RAM instead the File System.
Good luck!

.htaccess doesn't work on custom folders in codeigniter

I have my site running in Codeigniter. I have my own folder names custom where I have placed a simple .htaccess which has
Deny from all
But when i hit www.example.com/custom, it shows all files in browser. However, If I hit www.example.com/system, it says "Directory access is forbidden."
My folder structure is as follows.
mysite
mysite/system
mysite/custom
mysite/application
....
I have different .htaccess in root file and the redirection works fine.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
<IfModule mod_deflate.c>
# Compress HTML, CSS, JavaScript, Text, XML and fonts
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
AddOutputFilterByType DEFLATE application/x-font
AddOutputFilterByType DEFLATE application/x-font-opentype
AddOutputFilterByType DEFLATE application/x-font-otf
AddOutputFilterByType DEFLATE application/x-font-truetype
AddOutputFilterByType DEFLATE application/x-font-ttf
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE font/opentype
AddOutputFilterByType DEFLATE font/otf
AddOutputFilterByType DEFLATE font/ttf
AddOutputFilterByType DEFLATE font/woff
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE image/x-icon
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/xml
# Remove browser bugs (only needed for really old browsers)
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
Header append Vary User-Agent
ExpiresActive On
ExpiresDefault A0
<FilesMatch "\.(ico|ICO|pdf|PDF|flv|FLV|jpg|JPG|jpeg|JPEG|png|PNG|gif|GIF|js|JS|css|CSS|swf|SWF)(\.gz)?$">
ExpiresDefault "access plus 10 years"
Header append Cache-Control “publicâ€
Header unset ETag
FileETag None
Header set Expires "Thu, 15 Apr 2025 20:00:00 GMT"
</FilesMatch>
<FilesMatch “\.(xml|txt|html)$â€>
ExpiresDefault A7200
Header append Cache-Control “proxy-revalidateâ€
</FilesMatch>
<FilesMatch “\.(js|css)$â€>
ExpiresDefault A259200
Header append Cache-Control “proxy-revalidateâ€
</FilesMatch>
</IfModule>
I have the
Deny from all
in rest of the sub folders. System and application folders comes with CI. htaccess works fine in this folder. But in my own created folder "custom", htaccess file is not working.
What might be the problem? Why it allows htaccess work in the default folders and why it is not working in the user created folders
Note: I have same setting working in my nginx server. I have a different site Im uploading now. It is not working on the apache folder.
Try this code in your /custom/.htaccess:
RewriteEngine On
Deny from all
Delete your browser cache with (CTRL + F5) for WINDOWS or (Command + R) for MACOS and try again, make sure you have (dot) .htaccess and not just htaccess in your mysite/custom folder.

Gzip compression in multiple files

I am using Vanilla 2 forum and i noticed that my host does not support gzip.
I also noticed that in the php info file gzip is enabled.
So what i am trying to do is to put this line of code:
ob_start("ob_gzhandler");
at the very first line after the php tags. But there are almost 1000 php files in the whole vanilla script. Is there a way to automate this?
Or is there another alternative to gzip my website?
Update:
My .htaccess file:
# compress text, html, javascript, css, xml:
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
# Or, compress certain file types by extension:
<files *.html>
SetOutputFilter DEFLATE
</files>
There's the auto_prepend_file directive, which lets you force a particular chunk of code to be prependted to every file. You could put the ob code into that file, point PHP's prepend at it, and boom, gz output on all the scripts.

How do I site optimization php files with gzip compression using .htaccess or php.ini?

I'm using .htaccess provided by http://html5boilerplate.com/ which is correctly compressing the .html, .css, & .js files but I have not been able to get the .php files to compress unless I add <?php if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) ob_start("ob_gzhandler"); else ob_start(); ?> to the top of the page. The last thing I want to do is add that line to a hundred plus pages.
I've been using the following two sites to verify the gzip compression:
http://www.whatsmyip.org/http-compression-test/
http://www.gidnetwork.com/tools/gzip-test.php
The following suggestions have not worked either:
Gzip compression through .htaccess not working
How do you use gzip in .htaccess or php.ini?
I've also tried the following (http://perishablepress.com/fast-effective-php-compression/):
<?php // placed in a gzip_start.php file
ob_start("ob_gzhandler");
?>
<?php // placed in a gzip_stop.php file
ob_flush();
?>
# Placed in the .htaccess filew
# dual file includes for PHP compression
php_value auto_prepend_file /specify/full/path/to/gzip_start.php
php_value auto_append_file /specify/full/path/to/gzip_stop.php
Here's the Gzip section of my .htaccess file:
# ----------------------------------------------------------------------
# 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(gzip|deflate)?|X{4,13}|~{4,13}|-{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|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>
<IfModule !mod_filter.c>
# Legacy versions of Apache
AddOutputFilterByType DEFLATE text/text text/html text/plain text/css application/json
AddOutputFilterByType DEFLATE text/javascript application/javascript application/x-javascript
AddOutputFilterByType DEFLATE text/xml application/xml text/x-component
</IfModule>
# Webfonts and SVG:
<FilesMatch "\.(ttf|otf|eot|svg|php)$" >
SetOutputFilter DEFLATE
</FilesMatch>
</IfModule>
Edit Your .htaccess File like This
# compress text, html, javascript, css, xml:
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
# Or, compress certain file types by extension:
&ltfiles *.html &gt
SetOutputFilter DEFLATE
&lt/files&gt
&ltfiles *.css&gt
SetOutputFilter DEFLATE
&lt/files&gt
&ltfiles *.js &gt
SetOutputFilter DEFLATE
&lt/files&gt
&ltfiles *.img &gt
SetOutputFilter DEFLATE
&lt/files&gt
If you have views with .php extension. You will have to add this to .htaccess file
<files *.php >
SetOutputFilter DEFLATE
</files>

Categories