Coming off the heels of my SEO Task #1, Complete Permalinks, knowledgeable commenter Vanessa Fox said:
You want exactly one URL to every post. Pages can have multiple URLs pointing them. You want to avoid [this] by making sure there’s a unique URL for a page and that any other potential URLs for that page 301 redirect.
In my case the following two URLs pointed to the exact same content:
http://thepursuitofalife.com/2008/11/16/two-great-pictures/
and
http://thepursuitofalife.com/two-great-pictures/
I had enabled permalinks in WordPress, with a custom structure of /%postname%/. For some reason though, the version of the URL with year/month/day/postname/ redirected to /postname/
The following custom 404 Perl script fixed the problem: (whitespace removed for HTML purposes)
$qs = $_SERVER['QUERY_STRING'];
$pos = strrpos($qs, '://');
$found = 0;
$pos = strpos($qs, '/', $pos + 4);
$uri = substr($qs, $pos);
$pattern = '/(\d{4})\/(\d{2})\/(\d{2})(?P
if (preg_match($pattern, $uri, $groups)) {
$uri = $groups['title'];
$found = 1;
}
if ($found == 1) {
$host = $_SERVER['HTTP_HOST'];
$host = 'http://' . $host;
$uri = $host . $uri;
$loc = 'Location: ' . $uri;
header( "HTTP/1.1 301 Moved Permanently" );
header( "Status: 301 Moved Permanently" );
header( $loc ) ;
exit(0); // This is Optional but suggested, to avoid any accidental output
} else {
$_SERVER['REQUEST_URI'] = $uri;
$_SERVER['PATH_INFO'] = $_SERVER['REQUEST_URI'];
include('index.php');
}
?>
Originally I hadn’t put in the if (found == 1) part; but without it Firefox told me I was in an endless loop and couldn’t complete the request.
Now the incoming URL for the year/month/day/postname version has a 301 redirect to the “correct” version.
Fairly pleased so far.

