在 mod_rewrite 规则正则表达式中匹配问号 [英] Match Question Mark in mod_rewrite rule regex

查看:24
本文介绍了在 mod_rewrite 规则正则表达式中匹配问号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望用多个子字符串重写 url.一个子字符串作为子目录被请求,而任何其他子字符串作为普通查询字符串参数被请求.

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.

例如,我想重写来自

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]

如果我将问号更改为任何其他字符,效果很好.似乎问题在于?"字符被错误地解释为新查询字符串的开始.

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.

我需要按原样传递出现在/user/friends 之后的任何参数.我该如何实现?

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]

您的案例非常类似于在 mod_rewrite 说明书中使用 QSA 标志的示例.

这篇关于在 mod_rewrite 规则正则表达式中匹配问号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆