【问题标题】:.htaccess 301 redirect doesn't work due to question mark symbol "?"由于问号符号“?”,.htaccess 301 重定向不起作用
【发布时间】:2016-02-28 23:35:27
【问题描述】:

我正在使用以下代码到我的旧网站以 301 将所有流量重定向到我的新网站。

.htaccess - 位于 oldsite.com

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^newsite.com [NC] 
RewriteRule ^(.*)$ http://newsite.com/ [R=301,L]
</IfModule>
Redirect 301 / http://newsite.com/

上述 .htaccess 代码仅在 url 不包含问题掩码符号“?”时才有效。

例如

示例 1 可以

http://oldsite.com/whatever ->301 重定向到http://newsite.com/

但是,如果我要进入:

示例 2 不行

http://oldsite.com/?whatever ->301 重定向到http://newsite.com/?whatever

它应该像那样重定向到索引站点

http://oldsite.com/?whatever ->301 重定向到http://newsite.com/

示例 3 不行

http://oldsite.com/whatever?something ->301 重定向到http://newsite.com/?something

它应该像那样重定向到索引站点

http://oldsite.com/whatever?something ->301 重定向到http://newsite.com/

  • 为什么会这样?

  • 我该如何面对这个问题?

【问题讨论】:

    标签: apache .htaccess mod-rewrite redirect http-status-code-301


    【解决方案1】:

    重定向和重写规则都是多余的。像这样使用你的规则。要删除query string,您需要在重写规则的末尾添加一个问号。

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^newsite.com [NC] 
    RewriteRule ^(.*)$ http://newsite.com/? [R=301,L]
    </IfModule>
    

    【讨论】:

      猜你喜欢
      • 2013-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-06
      • 2017-06-30
      • 2014-07-05
      • 2014-12-21
      • 2011-03-08
      相关资源
      最近更新 更多