CURRENT PROJECTS
loading
CATEGORIES AND POSTS
loading
overset
DEVELOPMENT LOG FOR JIM PALMER
Posted 12/24/2007 in unix


An interesting problem with Apache's mod_cache (even "production" in the 2.2.X branch) is that it does not cache mod_dir's DirectoryIndex files. For instance, if you have a
DirectoryIndex index.html index.php
declaration, neither of which will be cached by mod_cache. I have only seen this be true for mod_disk_cache but assume it fails with mod_mem_cache as well. The reason for this is the very complex nature of mod_dir and how this would relate to caching of a file. There was a good email thread on dev@httpd.apache.org archived here: http://www.mail-archive.com/dev@httpd.apache.org/msg35291.html

I had to come up with a simple workaround - and the first thing to come to mind was trying to come up with a simple RewriteRule to circumvent the mod_dir DirectoryIndex. I came up with the following concoction which assumes that there is a
DirectoryIndex index.html index.php index.cfm
in the root httpd.conf declarations:
<VirtualHost *:80>
	#...
	# "disable" the default httpd.conf DirectoryIndex
	DirectoryIndex __NOFILE__
	# simulate DirectoryIndex index.html index.php index.cfm
	RewriteEngine On
	RewriteCond %{REQUEST_URI} ^(.*)/$
	RewriteCond "%{DOCUMENT_ROOT}%{REQUEST_URI}index.html" -f
	RewriteRule "^(.*)$" "$1index.html" [R=301,NE]
	RewriteCond %{REQUEST_URI} ^(.*)/$
	RewriteCond "%{DOCUMENT_ROOT}%{REQUEST_URI}index.php" -f
	RewriteRule "^(.*)$" "$1index.php" [R=301,NE]
	RewriteCond %{REQUEST_URI} ^(.*)/$
	RewriteCond "%{DOCUMENT_ROOT}%{REQUEST_URI}index.cfm" -f
	RewriteRule "^(.*)$" "$1index.cfm" [R=301,NE]
	#...
</VirtualHost>

This will force a SOE-compliant 301 Perm. Redirect for "directory" accesses with a valid existing [index.html|index.php|index.cfm] file - in that order. If neither of those 3 files exist, the normal order of operations will be carried out, i.e. directory listing if permissible, a forbidden error, a 404 missing error, etc.

This will also force mod_cache to work on your index files seeing as the actual REQUEST_URI contains a valid file. This should with with mod_mem_cache as well as mod_disk_cache. URL Strings will work with this and will be auto-appended to the new redirect location URL.

NOTE It is important to realize that any <FORM METHOD="POST"> submission with a directory action, i.e. <FORM ACTION="/subdir/" METHOD="POST"> will lose the form variables in the 301 redirection. I'd stay away from loading the index files via proxy and do a SEO-compatible 301 permanent redirection. This means that any action declaration in your HTML will require the filename it will be submitting to, i.e. <FORM ACTION="/subdir/index.php" METHOD="POST"> instead

ANOTHER NOTE Do not forget the NE declaration so to not foul your URL "GET" variables during the redirect.
comments
loading
new comment
NAME
EMAIL ME ON UPDATES
EMAIL (hidden)
URL
MESSAGE TAGS ALLOWED: <code> <a> <pre class="code [tab4|tabX|inline|bash]"> <br>
PREVIEW COMMENT
TURING TEST
gravatar