在写RewriteRule准备匹配url中的问号及后面的参数时,无法成功的原因。

  • RewriteRule 不会去匹配问号?后面的字符串,官方说明如下:

QSA|qsappend
When the replacement URI contains a query string, the default behavior of RewriteRule is to discard the existing query string, and replace it with the newly generated one. Using the [QSA] flag causes the query strings to be combined.

Consider the following rule:

RewriteRule “/pages/(.+)” “/page.php?page=$1” [QSA]
With the [QSA] flag, a request for /pages/123?one=two will be mapped to /page.php?page=123&one=two. Without the [QSA] flag, that same request will be mapped to /page.php?page=123 - that is, the existing query string will be discarded.

所以,解决方法是:(用其它字符代替,或者使用目录)

  • 示例:
RewriteRule ^(.*).(gif)!([\d]+)(x*)([0-9]*)$ /resize.php?file=$1&ext=$2 [L]
或者
RewriteRule   ^(\w{2,30})/(.+)$        index.php?controller=$1&action=$2 [QSA]