【问题标题】:How to fix URL canonicalization with .htaccess?如何使用 .htaccess 修复 URL 规范化?
【发布时间】:2010-11-12 10:26:26
【问题描述】:

我不希望服务器总是将我的 URL 重定向为“http://www.domain.com”格式,即使用户只写“domain.com”。

我可以在网上找到一些这样的例子,但是我已经在 .htaccess 文件中进行了一些修复,我不知道在哪里放置什么,所以它不会与之前的代码冲突。

这是我的 .htaccess 文件:

RewriteEngine on
RewriteBase /
ErrorDocument 404 /404

RewriteRule ^adminator/?$ adminator/login.php [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?detail1=$1&detail2=$2&detail3=$3&detail4=$4 [QSA,L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?detail1=$1&detail2=$2&detail3=$3 [QSA,L]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ index.php?detail1=$1&detail2=$2 [QSA,L]
RewriteRule ^([^/\.]+)/?$ index.php?detail1=$1 [QSA,L]

我应该输入什么,所以 URL 会重定向到“www”?

最后一个问题,我只需要这样做,这样搜索引擎就不会遇到 URL 问题吗?

【问题讨论】:

    标签: .htaccess url redirect canonical-link


    【解决方案1】:

    我通常会做相反的事情。

    您可能希望 www.example.com 转发到 example.com -- 较短的 URL 更性感。 没有-www.org/faq.php?q=class_b

    感谢HTML5 boilerplate,我通常会添加:

    <IfModule mod_rewrite.c>
      RewriteEngine On
      RewriteCond %{HTTPS} !=on
      RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
      RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
    </IfModule>
    

    如果你想在你的 .htaccess 中这样做:

    RewriteEngine on
    RewriteBase /
    ErrorDocument 404 /404
    RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
    RewriteRule ^adminator/?$ adminator/login.php [L]
    RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?detail1=$1&detail2=$2&detail3=$3&detail4=$4 [QSA,L]
    RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?detail1=$1&detail2=$2&detail3=$3 [QSA,L]
    RewriteRule ^([^/\.]+)/([^/\.]+)/?$ index.php?detail1=$1&detail2=$2 [QSA,L]
    RewriteRule ^([^/\.]+)/?$ index.php?detail1=$1 [QSA,L]
    

    否则,请尝试反向 RewriteCond 并关联 RewriteRule

    【讨论】:

      【解决方案2】:

      您的[^/\\.] 模式简化为[^/.],因为句点不需要转义。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-05-22
        • 2019-10-04
        • 2020-10-11
        • 1970-01-01
        • 2015-05-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多