【问题标题】:Redirect using .htaccess from a PHP page to another both with variables使用 .htaccess 从一个 PHP 页面重定向到另一个带有变量的页面
【发布时间】:2014-06-18 15:59:48
【问题描述】:

我有一个简单的问题,但我找不到解决方法。我需要重定向或重写这个:

http://mydomain.com/libro.php?isbn=9788493652470 

到这里:

http://mydomain.com/busqueda.php?busqueda=medimecum

我已经尝试过了,但它不起作用:

Redirect 301 /libro.php?isbn=9788493652470 http://mydomain.com/busqueda.php?busqueda=medimecum

【问题讨论】:

  • How to make a redirect in PHP? 的可能重复项
  • 这两个文件共享整个 head 部分,我也尝试了 PHP 解决方案“header()”,但它继续循环......我无法让它工作,即使使用“if ($_GET=9788493652470)"
  • 您无法匹配Redirect 指令中的查询字符串,请改用mod_rewrite 规则。
  • @anubhava 没错。使用RewriteCond 为查询字符串发布了答案。在我的测试中运行良好。

标签: php .htaccess redirect


【解决方案1】:

直接Redirect 不会很好用。但是使用mod_rewriteRewriteCond 作为查询字符串会很好。这应该适用于您的示例:

RewriteEngine On
RewriteCond %{QUERY_STRING} isbn=9788493652470
RewriteRule ^libro.php http://mydomain.com/busqueda.php?busqueda=medimecum [L,R=301]

现在再次查看您的问题,不清楚您是否要传递原始查询字符串,对吗?所以这个网址:

http://mydomain.com/libro.php?isbn=9788493652470

会变成这个网址:

http://mydomain.com/busqueda.php?busqueda=medimecum&isbn=9788493652470

但如果您想以某种方式这样做,您可以像这样将QSA(查询字符串追加)标志添加到RewriteRule

RewriteEngine On
RewriteCond %{QUERY_STRING} isbn=9788493652470
RewriteRule ^libro\.php http://mydomain.com/busqueda.php?busqueda=medimecum [L,R=301,QSA]

【讨论】:

  • +1 它应该可以工作,只需将正则表达式更改为^libro\.php$
  • @anubhava 谢谢!并注明。但它似乎与^libro.php 配合得很好。为什么要用^libro\.php 逃避它?
  • ^libro.php 可能有效,但也可以匹配 libro-php123 等。
猜你喜欢
  • 2014-09-01
  • 2012-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-19
  • 1970-01-01
  • 2014-08-10
  • 2013-02-08
相关资源
最近更新 更多