【问题标题】:Rewriting URLs with one query string to another URL with a different query string将具有一个查询字符串的 URL 重写为具有不同查询字符串的另一个 URL
【发布时间】:2013-04-05 12:39:04
【问题描述】:

我正在尝试完成一些重定向,如下所示:

#Basic Rule Set
RewriteRule ^/path$ http://newhost.com/Page?pagename=form1000 [R=301]
RewriteRule ^/path/index.html$ http://newhost.com/Page?pagename=form1000 [R=301]

效果很好。

但是,在源和目标中使用查询字符串进行重定向会将我返回到上述目标 url。例如,

# Advanced  Rule Set
RewriteRule ^/path/index.html?var=foo$ http://newhost.com/Page?pagename=form1000?id=hello  [R=301]
RewriteRule ^/path/index.html?var=bar$ http://newhost.com/Page?pagename=form1000?id=byebye [R=301]
RewriteRule ^/path/index.html?var=xyz$ http://newhost.com/Page?pagename=form1000?id=world  [R=301]

全部重定向到http://newhost.com/Page?pagename=form1000。 我在上述三个规则之前尝试了RewriteCond %{QUERY_STRING} ^var=(.*)$ [NC]RewriteCond %{REQUEST_URI} ^/path/index.html [NC],但我仍然被重定向到http://newhost.com/Page?pagename=form1000。我交换了基本规则集和高级规则集的顺序,并全部重定向到http://newhost.com/Page?pagename=form1000

关于如何让这些规则集发挥作用的任何想法? CentOS 6.x、Apache 2.2。

【问题讨论】:

  • 规则的 URI 路径中不存在查询。您必须使用RewriteCond %{QUERY_STRING} 对其进行测试。
  • 也许这个答案对stackoverflow.com/a/14899735/1741542有帮助
  • @faa:我确实用RewriteCond %{QUERY_STRING} ^var=(.*) [NC] 尝试过,结果相同。请注意,%1 未在目标 URL 中使用。
  • @faa:你删除了你的答案吗?我能够使用它来使事情正常进行。如果你愿意,再发一次,我会接受的。谢谢你的帮助! (-:

标签: mod-rewrite apache2.2


【解决方案1】:

你可以试试这个:

# Advanced  Rule Set
RewriteCond %{QUERY_STRING} var=foo [NC]
RewriteRule ^path/index.html/? http://newhost.com/Page?pagename=form1000?id=hello  [R=301,L,NC]

RewriteCond %{QUERY_STRING} var=bar [NC]
RewriteRule ^path/index.html/? http://newhost.com/Page?pagename=form1000?id=byebye [R=301,L,NC]

RewriteCond %{QUERY_STRING} var=xyz [NC]
RewriteRule ^path/index.html/? http://newhost.com/Page?pagename=form1000?id=world  [R=301,L,NC]

【讨论】:

  • 如果有循环,请告诉我。
  • @KM。这些规则适用于一个 .htaccess 文件。如果它们在主配置文件中,则必须在path 中添加一个前导斜杠,例如^/path...,就像您在问题中所说的那样。如果有任何问题,请告诉我。
  • 谢谢@faa,这正是我想要的。
  • 欢迎您@KM。感谢您在我删除后通知我。
猜你喜欢
  • 2015-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-25
  • 2015-12-26
  • 2017-06-21
相关资源
最近更新 更多