清理网址和重复页面(强制从头开始规范化)。

发布于 2024-11-26 17:06:15 字数 1652 浏览 0 评论 0原文

我正在尝试找到通过 htaccess 执行以下操作的方法。

  1. 删除 (.php) 扩展名
  2. 删除尾部斜杠 (/)
  3. 将前一个 (.php) 页面重定向到非 php 页面
  4. 将根索引的所有痕迹重定向到根域

以下是目录结构的示例。

"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.

  1. Remove (.php) extension
  2. Remove trailing slashes (/)
  3. Redirect former (.php) page to non-php page
  4. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你对谁都笑 2024-12-03 17:06:15

检查 %{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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文