比赛问号的mod_rewrite规则正则表达式 [英] Match Question Mark in mod_rewrite rule regex
问题描述
我期待重写与多个子网址。一子正在请求作为一个子目录,而其他任何被要求为正常的查询字符串参数。
I am looking to rewrite urls with multiple substrings. One substring is being requested as a subdirectory, while any others are requested as normal query string parameters.
例如,我想重写从URL中
For example, I would like to rewrite urls from
http://www.mysite.com/mark/friends?page=2
到
http://www.mysite.com/friends.php?user=mark&page=2
我能够用问号字符除外做到这一点。这里是我的重写规则:
I am able to accomplish this with the exception of the question mark character. Here is my rewrite rule:
...
RewriteEngine On
RewriteBase /
RewriteRule ^([A-Za-z0-9-_]+)/friends[?]?([^/\.]+)?$ friends.php?user=$1&$2 [L]
如果我更改了问号任何其它字符它的伟大工程。这似乎是问题是,'?'字符被除$ P $错误PTED作为一个新的查询字符串的开始
If I change the question mark to any other character it works great. It seems like the problem is that the '?' character is being interpreted incorrectly as the start of a new query string.
我需要传递后/用户/朋友说出现任何参数。我如何做到这一点?
I need to pass on any parameters that appear after /user/friends as is. How do I accomplish this?
推荐答案
您应该使用 [QSA]
标记,而不是试图重写查询字符串。 [QSA]
传递查询字符串来重写URL。
You should be using the [QSA]
flag instead of trying to rewrite the query string. [QSA]
passes on the query string to the rewritten URL.
所以,你的规则应该是这样的:
So your rule should look like:
...
RewriteEngine On
RewriteBase /
RewriteRule ^([A-Za-z0-9-_]+)/friends/? friends.php?user=$1 [QSA,L]
您情况下是非常相似的的使用QSA标志给出的href=\"http://rewrite.drbacchus.com/rewritewiki/Flags_2fQSA\">示例
Your case is very similar to the example given for using the QSA flag in the mod_rewrite cookbook.
这篇关于比赛问号的mod_rewrite规则正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!