Site directory changes made painless with htaccess
- Posted
29 January 2005
Previously the month in each URL of this site was represented with a number. On a whim I’ve decided it would be nicer and less ambiguous to use month names abbreviated to three characters. What might have been a major change is made minor with the addition of several lines to a .htaccess file. Requests for files residing in the old structure are redirected by apache to their new locations.
For example a clicking a link to an old entry regarding the last time I looked
at my archive structure:
https://www.ollicle.com/2003/09/02/no_more_archive.html
forwards you to the corresponding location in the new structure.
The instruction to do so is entered into a hidden text file in the root of the site directory.
This is what this file (called .htaccess) looks like to achieve this. There is a line for each month:
RedirectMatch permanent ^(.*)/([0-9]{4})/01/(.*)$ $1/$2/jan/$3
RedirectMatch permanent ^(.*)/([0-9]{4})/02/(.*)$ $1/$2/feb/$3
RedirectMatch permanent ^(.*)/([0-9]{4})/03/(.*)$ $1/$2/mar/$3
RedirectMatch permanent ^(.*)/([0-9]{4})/04/(.*)$ $1/$2/apr/$3
RedirectMatch permanent ^(.*)/([0-9]{4})/05/(.*)$ $1/$2/may/$3
RedirectMatch permanent ^(.*)/([0-9]{4})/06/(.*)$ $1/$2/jun/$3
RedirectMatch permanent ^(.*)/([0-9]{4})/07/(.*)$ $1/$2/jul/$3
RedirectMatch permanent ^(.*)/([0-9]{4})/08/(.*)$ $1/$2/aug/$3
RedirectMatch permanent ^(.*)/([0-9]{4})/09/(.*)$ $1/$2/sep/$3
RedirectMatch permanent ^(.*)/([0-9]{4})/10/(.*)$ $1/$2/oct/$3
RedirectMatch permanent ^(.*)/([0-9]{4})/11/(.*)$ $1/$2/nov/$3
RedirectMatch permanent ^(.*)/([0-9]{4})/12/(.*)$ $1/$2/dec/$3
I’m no regular expressions wizard – so there’s a good chance
that this can be done more elegantly. Trimming index.html
from
the monthly and daily archive links remains a task for another day.
Useful sources
The best documentation on redirection using this method is of course direct from Apache. This article on Retiring entries with Movable Type presents the interesting idea of using custom templates to create .htaccess files with Movable Type itself. Mark Pilgrim’s post, HTTP Error 410: Gone presented informative examples that helped me grasp what all this gear is about.