gpt4 book ai didi

regex - 在 mod_rewrite 规则正则表达式中匹配问号

转载 作者:行者123 更新时间:2023-12-03 11:48:03 25 4
gpt4 key购买 nike

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

例如,我想从

http://www.mysite.com/mark/friends?page=2


http://www.mysite.com/friends.php?user=mark&page=2

除了问号字符外,我能够完成此操作。这是我的重写规则:
...
RewriteEngine On
RewriteBase /
RewriteRule ^([A-Za-z0-9-_]+)/friends[?]?([^/\.]+)?$ friends.php?user=$1&$2 [L]

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

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

最佳答案

您应该使用 [QSA]标志而不是尝试重写查询字符串。 [QSA]将查询字符串传递给重写的 URL。

所以你的规则应该是这样的:

...
RewriteEngine On
RewriteBase /
RewriteRule ^([A-Za-z0-9-_]+)/friends/? friends.php?user=$1 [QSA,L]

您的情况与 the example given for using the QSA flag in the mod_rewrite cookbook 非常相似.

关于regex - 在 mod_rewrite 规则正则表达式中匹配问号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/822421/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com