【问题标题】:Specificity when redirecting dynamic PHP urls via htaccess通过 htaccess 重定向动态 PHP url 时的特殊性
【发布时间】:2014-10-28 10:05:04
【问题描述】:

我正在将站点布局从 example.com/product.php?id=1 更改为 example.com/new.php

我需要为这个从 id=1 到 new.php 的重定向保留一个 301 重定向规则。

我所拥有的,目前有效

RewriteCond %{query_string} id=1
RewriteRule (.*) https://example.com/new.php? [R=301,L]

需要解决的问题:

还有一个页面叫example.com/other-products.php?id=1

此 OTHER 页面上的所有产品也被重定向到“new.php”。

最后一个问题:

如何将特异性应用到此重定向,以便仅重定向以 products.php 开头的动态 url,而站点上具有相同 ID 号(例如 id=1)的所有其他动态 url 保持不变?

谢谢!

【问题讨论】:

  • 您可以使用多个条件。也许使用%{REQUEST_FILENAME} 来检查页面。所有先前的条件必须匹配才能运行规则。或者甚至只是将页面名称添加到规则中,而不是 (.*)

标签: php apache .htaccess mod-rewrite redirect


【解决方案1】:

您需要在RewriteRule 中使用正确的URI 模式:

RewriteCond %{query_string} ^id=1$ [NC]
RewriteRule ^product\.php$ https://example.com/new.php? [R=301,L,NC]

注意RewriteRule ^product\.php$,它只会影响/product.php?id=1 URL,但不会影响/product-abc.php?id=1 OR /product.php?id=123

【讨论】:

  • 这就是解决方案。我已将其输入该站点,进行了测试,并且运行良好。当计时器允许时,我会接受作为答案。谢谢好先生!你对我很有帮助。
猜你喜欢
  • 2013-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-05
  • 2015-10-01
  • 2021-06-30
  • 2016-09-18
  • 2021-04-10
相关资源
最近更新 更多