apache 重定向 URL(使用特定的 GET 参数)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/583705/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 17:19:00  来源:igfitidea点击:

Redirecting URLs (with specific GET parameters)

apachehttpredirect

提问by rlorenzo

I have this old survey link that is has been superseded by another link, so basically I want anyone trying to access the URL:

我有这个旧的调查链接已被另一个链接取代,所以基本上我希望任何人都试图访问该 URL:

http://mywebsite.com/survey/view_survey.php?surveyID=1

http://mywebsite.com/survey/view_survey.php?surveyID=1

To be redirected to:

要重定向到:

http://mywebsite.com/survey/view_survey.php?surveyID=2

http://mywebsite.com/survey/view_survey.php?surveyID=2

Can I do this in the Apache configuration or htaccess file?

我可以在 Apache 配置或 htaccess 文件中执行此操作吗?

I tried the following rule in the Redirect section of my httpd.conf file:

我在 httpd.conf 文件的重定向部分尝试了以下规则:

Redirect 301 /survey/view_survey.php?surveyID=1 http://mywebsite.com/survey/view_survey.php?surveyID=2

But it doesn't work. I am suspecting that the GET parameters are not used when processing the rule.

但它不起作用。我怀疑在处理规则时没有使用 GET 参数。

Is my only option to hack my code to redirect on a specific surveyID?

破解代码以重定向特定调查 ID 是我唯一的选择吗?



Following the suggestion of using the Rewrite rules, I tried the following in my .htaccess file:

按照使用重写规则的建议,我在我的 .htaccess 文件中尝试了以下内容:

RewriteRule ^survey/view_survey\.php\?surveyID=1525$ /survey/view_survey.php?sur
veyID=1607

But that doesn't work. I do have the rewrite engine up and running, because I have another rewrite rule currently running.

但这不起作用。我确实启动并运行了重写引擎,因为我当前正在运行另一个重写规则。

回答by Gumbo

Try this in a .htaccess file:

在 .htaccess 文件中试试这个:

RewriteEngine on
RewriteCond %{QUERY_STRING} (^|.*&)surveyID=1525(&.*|$)
RewriteRule ^survey/view_survey\.php$ /survey/view_survey.php?%1surveyID=1607%2 [L,R=301]

回答by Sean Bright

RewriteEngine On
RewriteCond %{QUERY_STRING} ^surveyID=1525$
RewriteRule ^/survey/view_survey\.php /survey/view_survey.php?surveyID=1607 [R=301]

回答by Jeremy L

Check out the QSA portion of the mod_rewrite.

查看 mod_rewrite 的 QSA 部分。

It does GET string manipulation.

它执行 GET 字符串操作。

回答by Erik ?erpnjak

There might be a possible duplicate of this question and it is solved if this solution doesnt work for you:

这个问题可能有重复,如果这个解决方案不适合你,它就解决了:

Apache Redirect 301 fails when using GET parameters, such as ?blah=

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