Autocreate mod_rewrite Mapping File

by john on February 7, 2003

In response to my article I Like mod_rewrite, Jonathon Delacour asks and I respond:

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? – Jonathon

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… 🙂 – john

However, in thinking about this a little I realized it should be possible to create a script that goes through each file (0000055.html, 000056.html, etc.) and extracts the information necessary to build the new URL, and automatically create the necessary mapping file for mod_rewrite. Such a script would simplify the process of moving to a new URL structure as Jonathon would like to do, especially for someone who has been a prolific writer and has hundreds or thousands of URLs to map.

Lazyweb, I invoke thee.

{ 6 comments }

Alex February 7, 2003 at 3:07 pm
john February 8, 2003 at 6:45 pm

That seems like a good approach, except for a one time only move I don’t think it make sense to incur the database hit on every future request, or am I missing something?

Now if you modified your script to actually create the static mapping file once, then I think that would answer the problem.

Bill Humphries February 9, 2003 at 3:46 am

I took a stab at your LazyWeb request: http://www.whump.com/www/lazyMTRedirect.html But it’s incomplete in that you wanted dates in the URL, and it’s probably better to keep the NNNNNN.html files and make the pretty URL a mapping.

I think Alex’s solution is much more elegant, because the ‘hit’ from going to the database is significantly less than parsing .htaccess for every request when the number of entries, and hence the mod_rewrite ruleset becomes larger than the source of the rewrite map.

I would, however, move it out of a rewrite map and into PHP because if a rewrite map script fails, it takes down your whole server.

Jonathon Delacour February 9, 2003 at 5:41 am

Only a matter of time

I’d pretty much resolved to retool my individual archives using the anders/gord dirified file naming system when Michael from i·me·michael tossed a spanner into the works (in the form of a first-rate piece of expository writing masquerading a…

Jonathon Delacour February 9, 2003 at 5:45 am

Only a matter of time

I’d pretty much resolved to retool my individual archives using the anders/gord dirified file naming system when Michael from i·me·michael tossed a spanner into the works (in the form of a first-rate piece of expository writing masquerading a…

john February 9, 2003 at 3:12 pm

Very nice Bill, Lazyweb comes through again! However, I’m inclined to now listen to you and Alex, and will be looking at using an archive program rather than an archive map.

Previous post:

Next post: