All of the blog archive files on this site have been renamed from *.html to *.php. If you encounter any problems with missing files, please let me know. If you are interested in how the redirects were done so that all the existing links and bookmarks to this site were not broken, keep reading.

Moving and renaming files is easy, but anyone coming from a bookmark or search engine link will be greeted with a 404 error unless something else is done. Enter mod_rewrite, a powerful Apache module. It can do a lot of great stuff, but what I needed to accomplish was fairly straightforward. For example, incoming requests for page.html should be redirected to page.php. There are several ways to accomplish this. The simplest way is to enter a redirect line in the .htaccess file for each renamed file. This site contains hundreds of files so editing the .htaccess file would have taken hours. A better option in this case was to insert a slightly more complex rule. The text of the rule I inserted in my .htaccess file appears below:

#   backward compatibility ruleset for
#   rewriting document.html to document.php
#   when and only when document.php exists
#   but no longer document.html
RewriteEngine on
RewriteBase   /blog/front/
#   parse out basename, but remember the fact
RewriteRule   ^(.*)\.html$              $1      [C,E=WasHTML:yes]
#   rewrite to document.php if exists
RewriteCond   %{REQUEST_FILENAME}.php -f
RewriteRule   ^(.*)$ $1.php                     [S=1,R=301]
#   else reverse the previous basename cutout
RewriteCond   %{ENV:WasHTML}            ^yes$
RewriteRule   ^(.*)$ $1.html

This rule is basically a cut and paste job from the Apache site with three exceptions:

  1. The RewriteBase directive needs to be changed to suit the site/directory the .htaccess file is living in.
  2. The example on the Apache site renames *.html to *.phtml so all instances of phtml have been changed to php.
  3. The option [S=1] was changed to [S=1,R]. At first, I found that while Firebird happily changed the URL in the address bar from *.html to *.php, Internet Explorer did not. I suspect that the return code passed back to the browser was not 301 – Moved Permanently. Adding the “R” option seems to have solved that. Update 2006-10-03: 3 years on, I’ve realized that “R” alone is not correct. One must also pass the “301” code, otherwise, it redirects without sending the “Moved Permanently” code. I’ve corrected the code snippet above to reflect this. Sorry…

Hopefully this page will save someone some headaches in the future if they find themselves in a similar situation.

One thought on “Site Changes

  1. […] Update: I spent a bit more time ensuring that all of my old posts from MT redirected to the new WP version. This is accomplished with a combination of mod_rewrite and Alex King’s Movable Type individual entry template and some mod_rewrite fun. After I rebuilt the old MT individual entries, I deleted just about everything but those files from the old blog. All seems to work well. I could have made the redirect for the old archive pages more graceful (they just redirect to the home page now), but I think it works well enough.   […]

Leave a Reply

Your email address will not be published. Required fields are marked *