Unbreaking Links - more PHP/MySQL

Wed Dec 19 12:52:24 EST 2001

In my last update I mentioned I added some MySQL and some more PHP to my Adventures section of my website (which you're reading). This broke all existing links to entries in this section from other sites. All the entries moved from something like http://www.chaosreigns.com/adventures/1999-12-02.php to something like http://www.chaosreigns.com/adventures/entry.php?date=1999-12-02&num=1.

I had set up an Apache RewriteRule to redirect people who were trying to access the old URLs to the index:

RewriteRule ^/adventures/200 /adventures/ [R]
RewriteRule ^/adventures/199 /adventures/ [R]


So any time someone tried to access a file that started with http://www.chaosreigns.com/adventures/199 or http://www.chaosreigns.com/adventures/200, they would be instantly and transparently redirected by my webserver to http://www.chaosreigns.com/adventures/. But this still did not take people to their desired destination, they had to figure out where to go from the index. For a few popular entries I created specific Apache Redirect entries:

Redirect /adventures/2001-04-17.php http://www.ChaosReigns.com/adventures/entry.php?date=2001-04-17&num=1

But this only worked for a few, because in a lot of cases I split the old entries up into several new entries.

http://www.chaosreigns.com/adventures/2001-06-15.php

became

http://www.chaosreigns.com/adventures/entry.php?date=2001-06-15&num=1
http://www.chaosreigns.com/adventures/entry.php?date=2001-06-15&num=2
http://www.chaosreigns.com/adventures/entry.php?date=2001-06-15&num=3


A friend of mine, Bill Jonas gave me a couple very good suggestions. He suggested that instead of redirecting old URLs to the index, I redirect them to a PHP script, and then from there try to figure out which new file the person was trying to get to. But when I redirected the old URLs to a PHP script, I could not find a way to figure out which entry the user was attempting to access - it didn't seem to be contained in any variable. Bill gave me another great suggestion: When redirecting to the PHP script, try to add the old URL as an argument to the PHP script. That worked:

RewriteRule ^/adventures/(200.*) /adventures/redirect.php?page=$1 [R]
RewriteRule ^/adventures/(199.*) /adventures/redirect.php?page=$1 [R]


So now, when someone tries to access http://www.chaosreigns.com/adventures/2001-06-15.php, they are redirected to http://www.chaosreigns.com/adventures/redirect.php?page=2001-06-15.php, and in redirect.php, I can try to figure out what entry the user is trying to get to.

I use a MySQL query to find all entries with the same date as the old file, and if there is only one, I use a meta refersh tag to redirect them to it after 3 seconds. If there is more than one date match, I display a table with dates and descriptions that are links to the corresponding new entries.

You can browse the old static html index, which still has it's old links, to see this redirection in action.

Unfortunately, I think the code involved is a little too specific to my needs, but if you feel that it would be useful to you, I will probably make it available.
next:lost 40 pounds in 6 months2002-07-14
previous:Added MySQL and more PHP to My Adventures - Next/Previous links2001-12-11

Comment on this page.
Return to Adventures index
Return to Darxus' home page.