【问题标题】:Problem with htaccess and query stringhtaccess 和查询字符串的问题
【发布时间】:2011-05-16 10:29:46
【问题描述】:

我正在尝试使用 mod_rewrite 重写一些 URL,但是我从两个相似的规则中得到了不同的结果。这是我的代码:

第一条规则:

RewriteEngine On  
RewriteBase /
RewriteRule ^api/(.*?)$            index.php?p=$1 [L]

输入 example.com/api/test1/test2/test3 我在 PHP 中得到以下输出:

Array ( [p] => test1/test2/test3 ) 

第二条规则:

RewriteEngine On  
RewriteBase /
RewriteRule ^/(.*?)$            index.php?p=$1 [L]

或

RewriteRule ^(.*?)$            index.php?p=$1 [L] #test

输入 example.com/test1/test2/test3 我在 PHP 中得到以下输出:

Array ( [p] => index.php ) 

这不是我所期望的,我认为它会像第一条规则一样[p] => test1/test2/test3。我该怎么做才能使结果相同?

提前致谢。

【问题讨论】:

  • 有什么问题?预期和会发生什么?
  • 我希望第二条规则返回与第一条规则相同的输出。你能听懂我说话吗?谢谢。

标签: .htaccess mod-rewrite query-string


【解决方案1】:

你的第二条规则,

RewriteRule ^(.*?)$ index.php?p=$1 [L]

包含一个与您正在重写的内容相匹配的测试模式 (index.php)。在将 /test1/test2/test3 初始重写为 index.php?p=test1/test2/test3 之后,执行另一次重写以转换 index.php (使用 p=test1/test2/test3 查询字符串)到 index.php?p=index.php。

您应该调整规则以便不执行第二次重写。一种方法是查看请求的目标是否存在:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*$ index.php?p=$0

【讨论】:

  • 如果这是解决方案,请设置为答案。
猜你喜欢
  • 2017-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-28
  • 2012-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多