Enable cache control (mod_expires) and gzip for site (blatmondo.x10.mx)

Status
Not open for further replies.

davidmck77

New Member
Messages
4
Reaction score
0
Points
0
Hi,

I wanted to add cache control and gzip compression to my site (blatmondo.x10.mx).

I've added this code to the .htaccess file, but it doesn't seem to work:
## Enable the mod_expires module
ExpiresActive On
## Set expiration date to 1 month for all style sheets and images
ExpiresByType text/css "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
## Set expiration date to 1 week for all HTML pages
ExpiresByType text/html "access plus 1 week"
ExpiresByType application/xhtml+xml "access plus 1 week"
### Apply a Cache-Control header to index.html
<Files index.html>
Header append Cache-Control "public, must-revalidate"
</Files>

## 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>

Am I doing this correctly?
Are these modules enabled for free hosting sites like mine?

There are about five threads on the forum asking the same question, but none of them have an answer.
Time t add it to the FAQs?

Kind regards,
David McKinnon
 
Last edited:

davidmck77

New Member
Messages
4
Reaction score
0
Points
0
Re: Enable cache control (mod_expires) and gzip or deflat for site (blatmondo.x10.mx)

OK, so I partly solved this by fixing my own stupid mistake. :p
Mac OS X doesn't allow files with a .prefix because they're reserved for system files and are usually invisible (I can probably change this in some way, but I'm not worrying about it now.)
Long story short, I loaded the file "htaccess" and, because cPanel doesn't automatically refresh, thought I was replacing the ".htaccess" file. So I loaded "htaccess" and renamed it to ".htaccess" and mod_expires works just fine.

Compression still doesn't work.
I tried changing deflate in the above example to gzip:

## Enable the mod_gzip module
<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>

but no joy.

Half fixed. Half to go. Any takers? :)
 
Last edited:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
mod_gzip and mod_deflate are not currently loaded on the free hosts. Instead, you can pass the files you wish to compress through a script. For example:

Code:
<IfModule !mod_gzip.c>
    RewriteCond %{HTTP:Accept-Encoding} (^|,)gzip|deflate(,|$)
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^/?(.*\.(html|js|css))$ /compress?u=/$1 [NS]
</IfModule>

compress.php:
PHP:
<?php
if ($_GET['u'][0] != '/') {
    $_GET['u'][0] = '/' . $_GET['u'][0];
}
// check that $_GET['u'] is safe to dump. For example:
$path = realpath(DOCUMENT_ROOT . $_GET['u']);
if (strpos($path, DOCUMENT_ROOT . '/') !== 0) {
    header('HTTP/1.0 404 Not Found');
    exit();
}
// you'd also need to implement HTTP authorization if you have any resources on your site that are protected by it.

if (isset($_SERVER['HTTP_ACCEPT_ENCODING'])) {
    if (preg_match('/(^|,)\s*gzip\s*(,|$)/', $_SERVER['HTTP_ACCEPT_ENCODING'])) {
        ob_start('ob_gzhandler');
    } else if (preg_match('/(^|,)\s*deflate\s*(,|$)/', $_SERVER['HTTP_ACCEPT_ENCODING'])) {
        if (function_exists('ob_deflatehandler')) {
            ob_start('ob_deflatehandler');
        } else {
            $path = "php://filter/read=zlib.deflate/resource=$path";
        }
    }
}
readfile($path);

To configure Finder to show dotfiles, use:
Code:
defaults write com.apple.Finder AppleShowAllFiles YES

You'll need to restart the Finder to see the change. You can do this with an applescript such as frogstomp's or the following:
Code:
set showFiles to ""
try
	set showFiles to do shell script ¬
		"defaults read com.apple.finder AppleShowAllFiles"
end try

set dfltBtn to 1

ignoring case
	if {showFiles} is in {"ON", "TRUE", "1"} then
		set dlgMsg to "Hidden Files (currently shown) ..."
		set dfltBtn to 2
	else if {showFiles} is in {"OFF", "FALSE", "0"} then
		set dlgMsg to "Hidden Files (currently hidden) ..."
	else
		set dlgMsg to "Hidden Files..."
	end if
	
	display dialog dlgMsg buttons {"Show", "Hide", "Cancel"} ¬
		default button dfltBtn
	copy the result as list to {buttonpressed}
	
	try
		set restartFinder to 1
		if the buttonpressed is "Hide" then
			do shell script ¬
				"defaults write com.apple.finder AppleShowAllFiles FALSE"
		else if the buttonpressed is "Show" then
			do shell script ¬
				"defaults write com.apple.finder AppleShowAllFiles TRUE"
		else if the buttonpressed is "Cancel" then
			set restartFinder to 0
		end if
	end try
end ignoring

if restartFinder is 1 then
	tell application "Finder" to quit
	delay 1
	tell application "Finder" to launch
end if
 

davidmck77

New Member
Messages
4
Reaction score
0
Points
0
Many thanks Misson, that's a very comprehensive answer, and it's nice to finally know for sure if mod_gzip was installed -- and the Terminal command worked very nicely :)

However, compression is still not working as far as I can tell.

Here's what I've done so far.
- added the
Code:
<IfModule !mod_gzip.c> ...
code to the .htaccess file.
- created a file called compress.php using the code you provided and placed it in the root directory with the .htaccess file.

Is that all I have to do?

I don't have any resources on the site which are protected by HTTP authorisation so I don't need to implement that.

Google Page Speed and Port 80's Compression check both report that compression isn't enabled.
Are these good ways to check?
 
Last edited:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
You can check it directly using telnet. Send an HTTP request, making sure to include the Host and Accept-Encoding headers, and examine the response. You should be doing this anyway as you develop the script. As it says in my sig, any code I post is an example rather than a solution simply to be dropped in place. For example, in this case I don't bother to set a body for the 404 response or the Content-Encoding header. For another thing, I don't always test the code; it may have bugs. The constant DOCUMENT_ROOT, for instance, is likely not defined in your set up. You may need to use $_SERVER['DOCUMENT_ROOT'] instead. As another example, if you're using clean/extensionless URLs, the implementations of that and the compression feature above may conflict.
 
Last edited:
Status
Not open for further replies.
Top