I Like mod_rewrite

by john on February 5, 2003

I’m really beginning to like the Apache module mod_rewrite. From the documentation:

This module uses a rule-based rewriting engine (based on a regular-expression parser) to rewrite requested URLs on the fly. It supports an unlimited number of rules and an unlimited number of attached rule conditions for each rule to provide a really flexible and powerful URL manipulation mechanism. The URL manipulations can depend on various tests, for instance server variables, environment variables, HTTP headers, time stamps and even external database lookups in various formats can be used to achieve a really granular URL matching.

I’m currently using mod_rewrite in two ways:

The first way I am using mod_rewrite is to account for changes I have made in my folder hierarchy and naming convention. Out of the box URLs for Movable Type look like this: http://www.johnsjottings.com/archives/000055.html. The first change I made improved the name of the actual file, which turned the URL into this: http://www.johnsjottings.com/archives/joe_millionaire.html. I then made a change to my folder hierarchy resulting in how my current URLs are built: http://www.johnsjottings.com/archives/2002/12/28/joe_millionaire.html.

If you click on any of those links you will be redirected to the most current format, the last one. How is this done? Through mod_rewrite. Why would you want to do that? Because once Google or another search engine indexes your site, or someone creates a specific link to a page on your site, you want to make sure that anyone who clicks through to that old URL is still served up the correct document. Sure you could keep all the old documents around but that wastes storage and is prone to error as you prune your site.

The other way I am using mod_rewrite is to cleanly redirect people who for some reason or another are clicking on a bad link. In my example, I posted a link to one of my articles on Usenet and my news poster chopped off the URL so it looked like: http://www.johnsjottings.com/archives/2003/02/01/craps_success_trip_report.h. Once I noticed the problem (via error.log) I simply created a mod_rewrite rule to redirect to the correct document without anyone being the wiser.

The rules for mod_rewrite can either be entered in httpd.conf or .htaccess and either directly or via a mapping file. I have not been able to get the mapping file working yet so for now I just build the rules right in the configuration file. Here is what the rules I refered to above look like:

RewriteRule ^/archives/000055\.html$ /archives/2002/12/28/joe_millionaire.html [R]
RewriteRule ^/archives/joe_millionaire\.html$ /archives/2002/12/28/joe_millionaire.html [R]
RewriteRule ^/archives/2003/02/01/craps_success_trip_report\.h$ /archives/2003/02/01/craps_success_trip_report.html [R]

It might wrap funny on your screen – that’s three lines, each beginning with “ReWriteRule”.

Absorb the documentation if interested but consider this warning from it:

But all this functionality and flexibility has its drawback: complexity. So don’t expect to understand this entire module in just one day.

Good luck.

{ 2 comments }

Jonathon Delacour February 7, 2003 at 6:42 am

John, does this mean I’d have to write over 800 rules in order to redirect all my current individual archive files? Or would the mapping file save me all that work?

john February 7, 2003 at 6:46 am

You will have 800 rules regardless of if you put in the httpd.conf or the mapping file. Because this uses regex (regular expressions) it can be very useful for making massive simple changes like moving all files from one folder to another, but impossible to benefit from regex when you need to make the sort of mapping changes you will need to make.

I think it boils down to how clean you want your directories. It’s not like you would have to do this all at once – you could leave the old stuff there and slowly add the mappings over time until you had them all. But I’m not sure if clean directories are worth that… 🙂

{ 1 trackback }

Previous post:

Next post: