Tim Habersack

Where I put my things..

Handling the URI structure change when moving to new CMS

Oct 25th 2011

I recently worked on an internal project that replaced a current web app. The new one had a cms behind it, and was overall a better product. So, we made it live, and moved the old tool. And all heck broke loose, since staff had bookmarks pointing to the old location all over the place.

Here is an example of the old link structure:

http://example.com/sub_dir/filename.html

The new link to the same content:

http://example.com/sub_dir/view.php?doc=filename

Now, sure I could try searching and replacing everywhere those links could live, but that sounds like a disaster. Instead I leveraged the mighty power of the mod rewrite in Apache.

I created a .htaccess file in the subdir/ directory. Then I added: [text] ReWriteEngine on RewriteBase /subdir RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*).html view.php?doc=$1 [/text]

1) We are turning on the rewrite engine. 2) setting the base directory as sub_dir. 3) Saying "If the requested file is not found..". 4) "Grab the text before the .html, and put it where the $1 is".

And bam! All those old links, when hit, point to the correct page. I thought this would be much harder than it was. I'm trying to tag the heck out of this post, so if others are looking to do the same thing, it is here for them.