使用GET参数时,Apache Redirect 301失败,例如?blah =

Mar*_*els 18 apache .htaccess seo redirect get

我为客户构建了一个新的PHP站点,并希望将排名最高的Google结果从旧站点结构重定向到新站点结构.

我在文档中将几十个Redirect 301放在.htaccess中,虽然有些工作正常,但我遇到了很多其他问题.

这很好用:

Redirect 301 /nl/flash/banner_new.swf http://www.example.com/actueel/nieuws.html?action=show&f_id=152
Run Code Online (Sandbox Code Playgroud)

这不起作用!(由于简单地跳过重定向,导致404):

Redirect 301 /nl/index.php?mID=24511&subID=0 http://www.example.com/solutions/printsolutions.html
Redirect 301 /nl/index.php?mID=24512&subID=0 http://www.example.com/support/koppeling-met-omgeving.html
Run Code Online (Sandbox Code Playgroud)

重定向在.htaccess文件中混合,只有带有GET参数的重定向才会失败.

有解决方法吗?忽略失败的重定向不是客户的选择.谢谢你的想法.

Mar*_*els 16

虽然Gumbo的答案的推理是正确的,但我无法让他的RewriteRule工作.

添加另一个RewriteCond做到了.以下测试并正常工作.

RewriteCond %{REQUEST_URI} /nl/index.php$
RewriteCond %{QUERY_STRING} ^mID=24511&subID=0$
RewriteRule ^.*$ http://www.example.com/solutions/printsolutions.html [L,R=301]
Run Code Online (Sandbox Code Playgroud)

  • `RewriteRule`的实际模式取决于它是在服务器配置中使用还是在.htaccess文件中使用,以及.htaccess文件所在的位置. (2认同)

Gum*_*mbo 12

Redirect 只对URL路径进行操作:

旧的URL路径是以斜杠开头的区分大小写(%-decoded)路径.[...]

因此,不检查URL查询(第一个?到第一个#之后的部分).

但是你可以使用mod_rewrite来做到这一点:

RewriteCond %{QUERY_STRING} ^mID=24511&subID=0$
RewriteRule ^nl/index\.php$ http://www.example.com/solutions/printsolutions.html [L,R=301]
RewriteCond %{QUERY_STRING} ^mID=24512&subID=0$
RewriteRule ^nl/index\.php$ http://www.example.com/support/koppeling-met-omgeving.html [L,R=301]
Run Code Online (Sandbox Code Playgroud)


小智 12

同意Gumbo和Martijn的答案......但是:

在Martijn的Typo中,应该有"^"来启动REQUEST_URI条件的正则表达式:

RewriteCond %{REQUEST_URI} ^/nl/index.php$
Run Code Online (Sandbox Code Playgroud)

我也只能在我的.htaccess文件所在的地方使用Martijn,而不是Gumbo.

另外,如果您不希望通过重写传递参数字符串,则应添加"?" 在URL的末尾:

RewriteRule ^.*$ http://www.example.com/solutions/printsolutions.html? [L,R=301]
Run Code Online (Sandbox Code Playgroud)

否则,按照Martijn的代码,它会显示"如果你的URL是/nl/index.php?mID=24511&subID=0,那么重定向到http://www.example.com/solutions/printsolutions.html?mID=24511&subID=0 with 301永久重定向标头,不处理此URL的更多规则"

这可能是也可能不是你想要的,并且作为一般规则,如果参数不被理解,它们将被简单地忽略而不会造成任何伤害,所以它可能无关紧要.但是,如果你想将一个人重定向到一个新页面并想要"漂亮的URL",那么剥离参数字符串是可取的,所以坚持使用"?" 在目标网址的末尾.