Static URLs are preferable to dynamic URLs that contain variables such as the page ID where search engines are concerned. It’s worth investing the time and/or money in setting up URL rewriting on your website so that www.example.com/index.php?pageID=1 can now be viewed at www.example.com/page-1.htm or even better at www.example.com/page-name.htm. The latter two URLs appear to be static HTML pages but using Apache’s mod_rewrite module and an .htaccess file you can serve the same content as the first URL. The next step is to ensure that anyone accessing the original URL is redirected to the correct new URL with a 301 HTTP status code.
SEO Unfriendly Pages
If you’ve got a CMS or some other sort of database of pages the easiest way to link to them will be something like www.example.com/index.php?pageID=1. After a while you might have started to think about search engine optimisation and concluded that www.example.com/page-1.htm would be a better URL format. You can rewrite the 1 in the URL onto index.php?pageID=1 very easily and achieve a website with static looking URLs in a short time.
Update Your Links
However, there will (hopefully) be lots of links out there on the web pointing at your old-style URLs. You don’t want to serve your page from both URLs, as this would lead to duplicate content and is something that should be avoided for SEO reasons. The best thing to do in this situation is to programme your script so that it will detect visitors coming from the old-style URLs and redirect them seamlessly to the correct new URL.
It would also be a good idea to contact other webmasters that are hosting links to your old-style URLs and ask them to update them with the new URLs.
Set Up Your Redirects
There are two ways that you could handle redirecting visitors from the old-style URLs to the new ones.
If you have simple static URLs that include the page ID in the file name (e.g. www.example.com/page-1.htm) then you can add a couple of lines to your .htaccess file and all of your old URLs will be redirected to the correct new URLs.
If you have a more advanced CMS or custom setup that allows you to specify page URLs then you will need to programme your script to detect visitors from old-style URLs, lookup the corresponding new URL and perform a redirect.
.htaccess Redirects
Redirecting /index.php?page=1 to /page-1.htm can be accomplished easily using Apache’s mod_rewrite functionality within your .htaccess file in the root of your website.
RewriteEngine On
RewriteCond %{QUERY_STRING} ^pageID=([0-9]+)$
RewriteRule index.php http://www.example.com/page-%1.htm? [R=301,L]
That code will redirect any old-style URL containing the pageID as a variable to the correct new URL.
Programmatical Redirects
If your new URLs won’t be as simple as /page-1.htm you probably won’t be able to simply set up an .htaccess rewrite rule. Instead you’ll need to take the pageID from the old-style URL and lookup the correct new URL from your database. Once you have the new URL you can forward the visitor with a 301 HTTP status code, which should keep both visitors and search engines happy. Given a little time, search engines should update their indexes to reflect the new URLs, too.
This code, based on the examples that we have been using, should give you an idea of how to set up your redirects:
<?php
$matches = array();
// Check whether the visitor is coming from an old-style URL with a pageID
// variable appended. Save the pageID so that we can use it to query the database.
if(preg_match('/index\.php\?pageID=([0-9]+)$/', $_SERVER['REQUEST_URI'], $matches))
{
// You should have a database connection open at this stage
$result = @mysql_query("SELECT `url` FROM `pages` WHERE `pageID` = {$matches['1']} LIMIT 1");
if($row = @mysql_fetch_assoc($result))
{
// We found the page, so redirect to it
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.example.com/{$row['url']}");
}
else
{
// No matching page was found
header("HTTP/1.1 404 Not Found");
header("Status: 404 Not Found");
// You could include a custom 404 error page here
die('404 Page Not Found');
}
}
else
{
// Grab the page content from your database
}
?>
Your page content starts here!
Once you have modified it to suit your individual setup, placing that code at the top of your script will detect visitors on the old-style URLs and redirect them seamlessly to the correct new URL. Remember that you’ll need your new URLs rewritten onto the script using Apache’s mod_rewrite and .htaccess in order to fetch the content.
Conclusion
It’s worth moving from dynamic URLs to static URLs. Using the techniques outlined above you should not be affected by duplicate content and visitors landing on old URLs should be forwarded to your new URLs without having to take any further action.


