Wrestling with Apache’s mod_rewrite
mod_rewrite is a great (read: extremely useful) module. For those who may be unfamiliar with its amazing abilities, unlike mod_alias, mod_rewrite actually re-writes a given url, so that
http://www.mydomain.com/some%20ridiculous%20url%20with%20spaces%20in%20it.html
turns into
http://www.mydomain.com/this
and other such wonderful things, completely replacing what would otherwise be visible to the browser's url bar and to search engines. Thus, this becomes a staple of the "Search Engine Friendly" url contingent for CMS-driven sites (such as those hosted under Joomla! and WordPress, for example).
I'm working on testing a new site which I am developing (more news on this as it nears launch), and while I really should be doing this monkeying around on my local Apache installation, I've been using the live server, utilizing a subdirectory of my own work domain. This by itself isn't really a problem, so I really don't think twice about doing such things, unless I'm really working on some new technology and may be concerned about an adverse reaction from PHP or Apache, itself.
So it came to be that I needed to enable SEF URLs in this new Joomla! installation. typically, this is accomplished by setting the appropriate options in the Joomla! 1.5 Global Configuration panel (including the "Use Apache mod_rewrite" option) and by renaming htaccess.txt to .htaccess in the root of the Joomla! site. However, when a Joomla! site is located in some subdirectory beneath the web root, it is often necessary to tweak the .htaccess file by adjusting the RewriteBase parameter to point to the Joomla! root, e.g.:
RewriteBase /some/directory/below/the/root
In my case, however, even though I knew that mod_rewrite was working for several other hosted domains in addition to my own, no matter what I did to .htaccess, I could not get SEF URLs to work (i.e., each time I clicked on something in one of the site menus, I was greeted by a 404 error). <sigh>
To verify that Apache was indeed processing mod_rewrite, I enabled rewrite logging by adding:
RewriteLog logs/rewrite_log RewriteLogLevel 9
to conf/extra/httpd-vhosts.conf (while many options for mod_rewrite may be entered in .htaccess, logging options - under Apache 2.2, at least - may only be specified in httpd.conf and/or httpd-vhosts.conf, requiring a restart/re-read of the configuration files by the Apache daemon). I carefully watched the log as I tested some random links from the site menus, and sure enough, mod_rewrite was processing the re-written URLs, though even at LogLevel 9, I wasn't seeing the logic being applied to the request.
Finally, it dawned on me to check the specifics in the vhosts-conf. Sure enough, the key was here:
<Directory "/APPS/Apache2/htdocs/mydomain.com"> Options FollowSymLinks MultiViews Includes AllowOverride None Order allow,deny Allow from all SetOutputFilter INCLUDES </Directory>
which is great as far as it goes (the Joomla! docs are emphatic about +FollowSymLinks), but the real killer is:
AllowOverride None
which requires an exception be added for the Joomla! site, as in:
<Directory "/APPS/Apache2/htdocs/mydomain.com/some/directory/below/the/root"> AllowOverride All </Directory>
(Now, the above could be tightened up to only allow override for the rewrite directives, but as I have in place a pretty tight .htaccess for this test site, I wasn't particularly concerned about leaving anything wide open. For more on the AllowOverride core directive, see here.)
Finally, it's important to turn off the rewrite log (or at least tone down the log level to 2 or below - 0 disables it).
I know that a number of people have struggled with getting SEF URLs working under various CMS frameworks. Hopefully, this will help someone else.
Last Updated on by
Leave a comment
You must be logged in to post a comment.