【问题标题】:Redirect non www to www issue将非 www 重定向到 www 问题
【发布时间】:2013-12-28 14:59:12
【问题描述】:

事实上,我正在处理一个小的 php 脚本,并且我正在使用像 rewrite 这样的 somme htacces 函数,但我有一个问题,为了让我使用这个代码将所有 urls dream-9.html 重定向到 dream.php?id=9 :

RewriteEngine On
RewriteRule dream-([0-9]+)\.html dream.php?id=$1 [L]

最近我决定将所有非 www 重定向到 www,但我在从非 www 重定向到 www 时遇到问题,dream-9.html 变为 dream.php?id=9 并且它显示在链接上,这是第二个代码:

RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^/?(.*) http://%1/$1 [L,R=301,NE] 

【问题讨论】:

    标签: .htaccess redirect


    【解决方案1】:

    您需要保持规则的正确顺序,即在内部重写规则之前保留 301 规则。

    有这样的规则:

    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
    
    RewriteRule dream-([0-9]+)\.html$ /dream.php?id=$1 [L,QSA,NC]
    

    【讨论】:

    • 它不会重定向到 www :(
    • 并使用L标志停止规则处理
    • 哦,我以为你想删除 www(根据你的规则)
    • 我想重定向到 www 并运行 dream-9.html as dream.php?id=9
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-13
    • 2023-04-03
    • 2018-06-06
    • 1970-01-01
    • 2012-11-08
    • 1970-01-01
    相关资源
    最近更新 更多