清理网址和重复页面(强制从头开始规范化)。
我正在尝试找到通过 htaccess 执行以下操作的方法。
- 删除 (.php) 扩展名
- 删除尾部斜杠 (/)
- 将前一个 (.php) 页面重定向到非 php 页面
- 将根索引的所有痕迹重定向到根域
以下是目录结构的示例。
"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)
更新:
这就是我当前的配置,使用 php 委托规范链接并执行重定向/显示 404。如果您发现问题或可以提供更好的解决方案,我将非常感激。现在,下面的代码可以工作,为每个 php 文件提供特定于该页面的规范链接函数。然后,该函数会识别当前页面是否与规范页面相同,如果不同,则会抛出错误,使目标页面成为唯一可以访问内容的页面。
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();
}
}
}
?>
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:
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();
}
}
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
检查
%{REQUEST_URI}
并执行 301 重定向(如果它不是您的首选网址)。另外,对于搜索引擎,请查看规范元标记。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.