Dealing with Vanished Pages

I use URL rewriting for the site I’m currently working on in order to both provide the users with more user-friendly addresses and improve results in search engines queries. However, since all the URI have been modified, I have to take into account :

  • that the users may have kept the former addresses in their bookmarks,
  • that the search engines still index the former addresses.

I have therefore been led to create a plain custom Error 301 page to deal with those two problems. It is not finished yet since it must still make the difference between the two cases, but it does a great job so far for redirecting the user on the right track – and the right page.

First of all, I added in my .htaccess file the former pages to associate them with the error 301:


RewriteEngine On

#Former URI
RewriteRule ^(.*)plan.php$ http://mydomain/?a=plan [R=301]
RewriteRule ^(.*)accueil.php$ http://mydomain/ [R=301]
RewriteRule ^(.*)rdv.php$ http://mydomain/?a=rdv [R=301]
[…]

Those pages are redirected with a 301 error code.

Then, I must specify the address of my brand new 301 error page:

# Any other name would suit better, I guess
ErrorDocument 301 /301.php

And here’s what I do so far in this page:

<?php
$uri_deplacee = $_SERVER['REQUEST_URI'];
$nlle_uri = "";
$prefix = "/";
if (strpos($uri_deplacee, "news.php") > 0)
{
ereg("id_news=([0-9]*)$", $uri_deplacee, $regs);
$nlle_uri = $prefix."news,".$regs[1];
}
else 
{
switch ($uri_deplacee)
{
case $prefix."plan.php" : $nlle_uri = $prefix."plan"; break;
case $prefix."accueil.php" : $nlle_uri = $prefix; break;
case $prefix."rdv.php" : $nlle_uri = $prefix."rendez_vous"; break;
default: break;
}
}
?>

The first if checks if the page is news.php which is a page displaying the news item whose id is provided by the query parameter id_news. Since the news pages now call the news this way: http://mydomain/news,45, a little regexp is necessary to strip all the useless bits and find the id of the news to build the new URI. The else is a plain switch to redirect to the right page. I’m sure there is a way to use the URI used for the redirection in the .htaccess file, but I haven’t thought that far yet.

Then, the page displays a little text warning the user and redirects towards the right page thanks to the following meta:

<meta http-equiv="refresh" content="3;URL=<?=$nlle_uri?>" />

Don’t forget to provide the user with a link to $nlle_uri as well, just in case the redirection wouldn’t work.

The next step is to analyse the referer to the page, in order to know whether the user came here via a search engine or simply because of his/her outdated bookmark. More refinements to come, then.

 
---

Commenting is closed for this article.

---