【问题标题】:Redirecting URLs (with specific GET parameters)重定向 URL(使用特定的 GET 参数)
【发布时间】:2010-10-09 16:21:43
【问题描述】:

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

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

要重定向到:

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

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

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

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

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

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


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

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

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

【问题讨论】:

  • 据我所知,为什么获取 .htaccess 文件比一行 php 更容易?

标签: apache http redirect


【解决方案1】:

在 .htaccess 文件中试试这个:

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

【讨论】:

  • 没有成功。我陷入了一个循环。 URL 是 /survey/view_survey.php?surveyID=1607&surveyID=1607(.. 重复了很多次)&surveyID=1525 FireFox 给了我错误:重定向循环 Firefox 检测到服务器正在以某种方式重定向该地址的请求那永远不会完成。
  • 似乎是 QSA 标志导致了这种行为。
【解决方案2】:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^surveyID=1525$
RewriteRule ^/survey/view_survey\.php /survey/view_survey.php?surveyID=1607 [R=301]

【讨论】:

    【解决方案3】:

    查看 mod_rewrite 的 QSA 部分。

    它执行 GET 字符串操作。

    【讨论】:

    • QSA 在这里没有帮助,因为匹配模式是错误的
    【解决方案4】:

    此问题可能存在重复,如果此解决方案不适合您,则已解决:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-21
      • 1970-01-01
      • 2017-02-26
      • 2016-03-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多