【问题标题】:htaccess url rewrite url containing question markhtaccess url重写包含问号的url
【发布时间】:2026-01-06 20:40:02
【问题描述】:

我在重定向一些网址时遇到问题,如下所示,有许多不同猫品种的网址。谁能帮忙解决这个问题。

重定向网址示例:

http://www.exampledomain.co.uk/cats/database.nsf/catsforsale!openform?Breed=Persian

我希望它指向下面的 url。然后我的 php 脚本应该做一些更复杂的重定向以使 url 整洁:

http://www.exampledomain.co.uk/display_pets.php?pettype=Cats&petbreed=Persian

我尝试了下面的重写,但它不起作用,它根本不重定向,我认为它可能与? :

RewriteRule ^/cats/database.nsf/catsforsale!openform?Breed=(.*)$ display_pets.php?pettype=Cats&petbreed=$1 [L]

【问题讨论】:

  • RewriteRule 仅匹配 URL 的路径部分。如果要检查查询字符串内容,则需要使用 RewriteCond。

标签: php apache .htaccess redirect mod-rewrite


【解决方案1】:

RewriteRule 通常不查看查询字符串。

您需要将 QSA 标志与 Rewrite 一起使用。它结合查询字符串并将其附加到目标 url。

您可以尝试以下方法:

RewriteRule ^(.*)$ display_pets.php?url=$1 [L]

这会将整个源 url 传递给目标 url 的 url 参数。它还将源 url 的查询字符串附加到目标 url。

另一种选择是匹配 RewriteCond 中的查询字符串。

RewriteCond %{QUERY_STRING} url=(.*)
RewriteRule ^/cats/database.nsf/catsforsale!openform$  display_pets.php?pettype=Cats&petbreed=%1 [L]

查看以下链接:

RewriteRule FlagsManipulating the Query String

【讨论】: