使用 GET 参数时,Apache Redirect 301 失败,例如 ?blah=
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1260632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Apache Redirect 301 fails when using GET parameters, such as ?blah=
提问by Martijn Heemels
I've built a new PHP site for a customer and want to redirect the top ranking Google results from the old site structure to the new one.
我为客户建立了一个新的 PHP 站点,并希望将排名靠前的 Google 结果从旧站点结构重定向到新站点结构。
I've put several dozen Redirect 301's in a .htaccess in the documentroot, and while some work fine I'm having issues with a bunch of others.
我已经在 documentroot 的 .htaccess 中放置了几十个 Redirect 301,虽然有些工作正常,但我遇到了其他一些问题。
This works fine:
这工作正常:
Redirect 301 /nl/flash/banner_new.swf http://www.example.com/actueel/nieuws.html?action=show&f_id=152
This doesn't work! (leading to a 404 since the redirect is simply skipped):
这不起作用!(导致 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
The redirects are mixed in the .htaccess file, and only the redirects with GET parameters appear to fail.
重定向在 .htaccess 文件中混合,只有带有 GET 参数的重定向似乎会失败。
Is there a workaround? Ignoring the failing redirects is not an option to the customer. Thanks for your thoughts.
有解决方法吗?忽略失败的重定向不是客户的选择。谢谢你的想法。
采纳答案by Martijn Heemels
While Gumbo's answer'sreasoning was correct, I could not get his RewriteRule to work.
虽然Gumbo 的回答的推理是正确的,但我无法让他的 RewriteRule 起作用。
Adding another RewriteCond did it. The following was tested and works fine.
添加另一个 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]
回答by user204529
Agreeing with both Gumbo's and Martijn's answers ... but:
同意 Gumbo 和 Martijn 的回答……但是:
Typo in Martijn's, there should be be "^" to start the regular expression for the REQUEST_URI condition:
Martijn 的打字错误,REQUEST_URI 条件的正则表达式应该以“^”开头:
RewriteCond %{REQUEST_URI} ^/nl/index.php$
I too could only get Martijn's, not Gumbo's, to work where my .htaccess file was.
我也只能让 Martijn 的,而不是 Gumbo 的,在我的 .htaccess 文件所在的地方工作。
Also, if you don't want the parameter string to be passed on with the rewrite, you should add a "?" on the end of the URL:
此外,如果您不想在重写时传递参数字符串,您应该添加一个“?” 在 URL 的末尾:
RewriteRule ^.*$ http://www.example.com/solutions/printsolutions.html? [L,R=301]
Otherwise, following Martijn's code, it reads "if your URL is /nl/index.php?mID=24511&subID=0 then redirect to http://www.example.com/solutions/printsolutions.html?mID=24511&subID=0with a 301 Permanent redirect header and don't process more rules on this URL"
否则,下面的Martijn的代码,它读取“如果您的网址是/nl/index.php?mID=24511&subID=0然后重定向到http://www.example.com/solutions/printsolutions.html?mID=24511&subID=0与301 永久重定向标头,不要在此 URL 上处理更多规则”
This may or may not be what you want, and to be fair as a general rule if parameters are not understood they will simply be ignored without doing any harm, so it probably won't matter. However if you're wanting to redirect a human to a new page and want "pretty URLs" then stripping off the parameter string is preferable, so stick the "?" on the end of the destination URL.
这可能是您想要的,也可能不是您想要的,并且作为一般规则,如果不理解参数,它们将被简单地忽略而不会造成任何伤害,因此这可能无关紧要。但是,如果您想将一个人重定向到一个新页面并想要“漂亮的 URL”,那么最好去掉参数字符串,所以坚持使用“?” 在目标 URL 的末尾。
回答by Gumbo
Redirectdoes only operate on the URL paths:
Redirect只对 URL 路径进行操作:
The old URL-pathis a case-sensitive (%-decoded) path beginning with a slash. […]
旧的URL-path是一个以斜杠开头的区分大小写(%-decoded)的路径。[…]
So the URL query (the part after the first ?up to the first #after) is not checked.
因此不检查URL 查询(第一个?到第一个#之后的部分)。
But you can use mod_rewriteto do that:
但是你可以使用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]

