【问题标题】:Error trying redirect www to non www using htaccess尝试使用 htaccess 将 www 重定向到非 www 时出错
【发布时间】:2014-05-15 10:18:01
【问题描述】:

我在尝试使用 htaccess 将 www 重定向到非 www 时遇到一个问题。

这是我的 .htaccess 代码:

    Options All -Indexes
    Options +FollowSymLinks -MultiViews    

    RewriteEngine On
    RewriteBase /

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

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

当我访问http://www.zipurl.me/developer 时,我会重定向到http://zipurl.me/developer。 但是当我访问我的主页时,http://www.zipurl.me 我没有重定向到http://zipurl.me

有什么建议吗?

【问题讨论】:

    标签: apache .htaccess mod-rewrite redirect web


    【解决方案1】:

    问题是这两个重写条件基本上阻止了主页被重定向。

    用这个替换你的代码:

    Options All -Indexes -MultiViews
    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L,NE]
    

    【讨论】:

    • 谢谢。像魅力一样工作!
    • 很高兴知道,您能否通过单击我的答案左上角的勾号将答案标记为已接受。
    • 使用此代码,我无法检索 PHP POST 变量。有什么建议吗?
    • 您可以使用附加条件:RewriteCond %{REQUEST_METHOD} !=POST 来防止它被重定向到 POST 请求。
    猜你喜欢
    • 2012-10-19
    • 1970-01-01
    • 2012-11-16
    • 2018-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-12
    • 2015-12-31
    相关资源
    最近更新 更多