note 44234 deleted from function.header by tomsommer
| From: | tomsommer@php.net | Date: | Mon, 19 Jul 2004 10:17:30 +0000 |
| Subject: | note 44234 deleted from function.header by tomsommer | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-73596@lists.php.net to get a copy of this message | ||
Note Submitter: pediddle@pediddle.net
----
On one of my webpages, I often use <base href="..."> to simplify management of
intra-site links. This works fine in almost all cases, but some very old browsers, and more
importantly, nearly *all* search crawlers don't honor the base href. This often leads to
"doubled" URLs such as "/home/home/foo" instead of "/home/foo".
Here's a little script to rewrite the former into the latter, intended to be used as your
server's 404 error page.
<?php
$redirect = 'http://';
$redirect .= $_SERVER['HTTP_HOST'];
if (preg_match('#(.*/([-A-Za-z0-9$_.+!*\'(),]+))/\2(/.*)#',
$_SERVER['REQUEST_URI'], $matches)) {
$redirect .= $matches[1];
$redirect .= $matches[3];
header("Location: $redirect");
header("Status: 301 Moved Permanently");
} else {
$redirect .= dirname($_SERVER['SCRIPT_NAME']);
$redirect .= '/404.html';
header("Status: 404 Not Found");
@readfile($redirect);
}
?>