Thursday, November 13, 2008

Gotcha Of The Day - Dealing with Legacy File Names

Let's say you're dealing with existing website, and you have a page like contact.html. Now, let's say you would like to have that page in PHP so that you can programatically render the list of users to contact.

Just renaming the file from contact.html to contact.php will leave you in a heap of trouble, because any links to this file will suddenly be broken. If the site is small, you can just update the files - but in a large, unknown legacy site, this isn't possible.

It hit me, while I was facing this exact problem, that there's an elegant solution. Rename the file, then upload a .htaccess file with the following in it:

 RewriteEngine on
 RewriteRule contact.html contact.php

And there you have it - the world can access contact.html, yet contact.php will really be invoked.

I'd say, if you do any web development, among the top 3 skills you should have should be writing Apache Rewrite Rules. Any effort you put in to learning them will be handsomely returned to you.

No comments:

Post a Comment