Changing your URLs: Mapping old URLs to new ones
Users wishing to change their URL structure for their blog should approach this process carefully because a change like this without planning may result in:
- readers being unable to find your content
- orphaned files on your file server whose content will be stale over time
- search engines penalizing you for having replicated content on your web site
To mitigate these risks it is often a good idea to provide your web server with a mapping file that will help redirect your readers from your old URL structure to your new one. The following document will provide you with guidance and sample code for how to complete this process.
This guide however will require customization on your part, as every user’s URL structure can be very different from one another.
Creating a URL Mapping File
This describe a process for users of the Apache web server who have the mod_rewrite plugin installed and enabled. The process involves creating an Index Template that iterates over every file in your blog and outputs a RewriteRule to map the old URL to the new URL for each file.
How it works
- The
MTIfArchiveTypeEnabled
tag is used to conditionally output rewrite rules for each of the allowable archive types. - The
MTArchiveList
tag is responsible for iterating over each file associated with that archive type. - The
MTFileTemplate
tag is used twice per file, once to output the URL format for the old structure, and again for the new structure. - Archive File Path Specifiers are used to specify the format of each URL output by the
MTFileTemplate
tag.
Instructions
- Create an Index Template called “Rewrite Rules”
- Set the output file name to
rewrite_rules.conf
- Cut and paste and then customize the template code below into the index template
- Publish the template
- In your
httpd.conf
file or your.htaccess
file, include the following text: Include /path/to/your/rewrite_rules.conf` - Restart Apache
When you are satisfied that everything is working, you may delete the template you created in step #1 above. But keep the rewrite_rules.conf
file around forever. It will keep all of your old URLs alive forever.
Sample Template
RewriteEngine on
<MTIfArchiveTypeEnabled archive_type="Individual">
<MTArchiveList archive_type="Individual">
RewriteRule ^<mt:FileTemplate format="/%c/%i" />$ <$mt:BlogURL$><mt:FileTemplate format="%-c/%i" /> [L,R]</MTArchiveList>
</MTIfArchiveTypeEnabled>
<MTIfArchiveTypeEnabled archive_type="Monthly">
<MTArchiveList archive_type="Monthly">
RewriteRule ^<mt:FileTemplate format="/%y/%m/%i" />$ <$mt:BlogURL$><mt:FileTemplate format="%-c/%y/%m/%i" /> [L,R]</MTArchiveList>
</MTIfArchiveTypeEnabled>
<MTIfArchiveTypeEnabled archive_type="Category">
<MTTopLevelCategories>
<MTIfNonZero tag="MTCategoryCount">
RewriteRule ^<mt:FileTemplate format="/%c/%i" />$ <$mt:BlogURL$><mt:FileTemplate format="%-c/%i" /> [L,R]</MTArchiveList>
</MTIfNonZero>
</MTTopLevelCategories>
</MTIfArchiveTypeEnabled>
<MTIfArchiveTypeEnabled archive_type="Author">
<MTArchiveList archive_type="Author">
RewriteRule ^<mt:FileTemplate format="/%a/%i" />$ <$mt:BlogURL$><mt:FileTemplate format="%-a/%i" /> [L,R]</MTArchiveList>
</MTIfArchiveTypeEnabled>
<MTIfArchiveTypeEnabled archive_type="Category-Monthly">
<MTArchiveList archive_type="Category-Monthly">
RewriteRule ^<mt:FileTemplate format="/%c/%y/%m/%i" />$ <$mt:BlogURL$><mt:FileTemplate format="%-c/%y/%m/%i" /> [L,R]</MTArchiveList>
</div>
</MTIfArchiveTypeEnabled>
<MTIfArchiveTypeEnabled archive_type="Author-Monthly">
<MTArchiveList archive_type="Author-Monthly">
RewriteRule ^<mt:FileTemplate format="/%a/%y/%m/%i" />$ <$mt:BlogURL$><mt:FileTemplate format="%-a/%y/%m/%i" /> [L,R]</MTArchiveList>
</MTIfArchiveTypeEnabled>
Sample Output
RewriteRule ^/television/2008/02/index.php$ http://www.majordojo.com/television/2008/02/index.php [L,R]
RewriteRule ^/blogging/2008/02/index.php$ http://www.majordojo.com/blogging/2008/02/index.php [L,R]
RewriteRule ^/blogging/2008/01/index.php$ http://www.majordojo.com/blogging/2008/01/index.php [L,R]
RewriteRule ^/current_events/2008/01/index.php$ http://www.majordojo.com/current-events/2008/01/index.php [L,R]
RewriteRule ^/geeky_goodness/2008/01/index.php$ http://www.majordojo.com/geeky-goodness/2008/01/index.php [L,R]
RewriteRule ^/aint_it_cool/2008/01/index.php$ http://www.majordojo.com/aint-it-cool/2008/01/index.php [L,R]
RewriteRule ^/blogging/2008/01/index.php$ http://www.majordojo.com/blogging/2008/01/index.php [L,R]
RewriteRule ^/current_events/2008/01/index.php$ http://www.majordojo.com/current-events/2008/01/index.php [L,R]
RewriteRule ^/television/2008/01/index.php$ http://www.majordojo.com/television/2008/01/index.php [L,R]
RewriteRule ^/its_funny_laugh/2007/12/index.php$ http://www.majordojo.com/its-funny-laugh/2007/12/index.php [L,R]
RewriteRule ^/science_fiction/2007/12/index.php$ http://www.majordojo.com/science-fiction/2007/12/index.php [L,R]
RewriteRule ^/television/2007/12/index.php$ http://www.majordojo.com/television/2007/12/index.php [L,R]
RewriteRule ^/aint_it_cool/2007/12/index.php$ http://www.majordojo.com/aint-it-cool/2007/12/index.php [L,R]
RewriteRule ^/aint_it_cool/2007/12/index.php$ http://www.majordojo.com/aint-it-cool/2007/12/index.php [L,R]
RewriteRule ^/its_funny_laugh/2007/12/index.php$ http://www.majordojo.com/its-funny-laugh/2007/12/index.php [L,R]
Additional Resources
- Upgrade Guide
- Archive File Path Syntax
- FileTemplate Documentation
- Clean Sweep Plugin - a plugin to report 404s and keep pages alive
budgibson.myopenid.com on May 2, 2008, 9:44 a.m. Reply
One issue with your template is that it does not include the case for subcategories. Also, you have a couple of typos. For instance, a closing div tag somehow wandered into the middle of one of the rules when I copied it. Also, there is an extra MTArchiveList tag in the rules for categories. Here’s my own version of this template that allows for the case of subcategories and seems to work. Same caveats as with yours. People will need to modify.
Ann Ray on May 20, 2008, 10:11 a.m. Reply
mod_rewrite is really meant for ongoing aliasing, such as shortening a path or hiding an extension. And step 6 is a wee problem for those of us on shared hosts.
Here’s an alternative when you just want to send people to the new location. And unlike mod_rewrite, this sends a 301 code indicating the move is permanent.
http://httpd.apache.org/docs/2.2/mod/mod_alias.html#redirectpermanent
It can be tedious to create the list, but you can always just map the most recent/popular articles and let the rest go to a custom 404. In my site’s root htaccess, I just added one line per article: