Cleaning up urls and duplicate pages (forcing canonical from start).
I am trying to find ways do the following via htaccess.
- Remove (.php) extension
- Remove trailing slashes (/)
- Redirect former (.php) page to non-php page
- Redirect all traces of the root index to the root domain
Here is an example of the directory structure.
"http://example.com" (canonical - this is what I want)
"http://example.com/index" (redirect or 404)
"http://example.com/index/" (redirect or 404)
"http://example.com/index.php" (redirect or 404)
"http://example.com/index.php/" (redirect or 404)
"http://example.com/about" (canonical - this is what I want)
"http://example.com/about/" (redirect or 404)
"http://example.com/about.php" (redirect or 404)
"http://example.com/about.php/" (redirect or 404)
UPDATE:
开发者_StackOverflow中文版This is what my current configuration looks like, using php to delegate the canonical link and preform redirect / show 404. if you find a problem with it or can offer a better solution I would really appreciate it. Right now the code below works, by giving each php file it's own canonical link function specific to the page. The function then recognizes if the current page is the same as the canonical and if not will throw an error, leaving the intended page the only one that can access the content.
ErrorDocument 404 /404.php
RewriteEngine On
#removes trailing slash
RewriteCond %{HTTP_HOST} ^localhost:8888$
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
#removes php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
<?php
function resolve_canonical($canonical,$location = "404"){
$current_url= "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
if($current_url !== $canonical){
if($location == "404"){
//header("location: http://".$_SERVER['HTTP_HOST']."/404");
//header("HTTP/1.0 404 Not Found");
readfile('404.php');
exit();
}elseif($location == "redirect"){
header("location: ".$canonical);
exit();
}
}
}
?>
Examine %{REQUEST_URI}
and do a 301 redirect if it isn't your preferred URL. Also, for search engines look at the canonical meta tag.
精彩评论