在我的.htaccess文件中,我定义了以下规则,
In my .htaccess file I have defined following rule,
RewriteRule ^([-0-9a-zA-Z]+) search.php?id=$1如果我正在浏览 example/abcd 我需要使用符号&网址中的%-/ ,例如: example/ab&cd
The above rule works fine if I am browsing example/abcd I need to use the symbols & % - / in the url like: example/ab&cd
此规则必须进行哪些更改才能起作用?
What changes have to be made to the rule for this to work?
推荐答案不知道该规则如何为您工作。首先,它循环。其次,对于 $ 2 和 $ 3 没有捕获组,但这并不重要,因为 $ 1 始终始终是搜索。我假设您粘贴了一条行之有效的规则的部分代码。
No idea how that rule is working for you. First, it loops. Second, there is no capture groups for $2 and $3, but it doesn't matter because $1 is always "search" anyways. I'm assuming you've pasted a partial snippet of a rule that you have that works.
& ,%或 / 不匹配是因为您的正则表达式说:
The reason why &, %, or / isn't being matched is because your regex says:
[-0-9a-zA-Z]+表示:一个或多个字母,数字或破折号。因此,没有& ,%或 / 。因此,您可以将其添加到方括号中:
which means: one or more letters, numbers, or a dash. So no &, %, or /. So you can add those into the square brackets:
RewriteRule ^([-0-9a-zA-Z/%&]+) search.php?id=$1&ff=$2&ffid=$3但是,请记住,在应用任何规则之前,已先对URI进行解码。这意味着如果URI看起来像这样:
However, keep in mind that the URI is decoded before any rules get applied. This means if the URI looks like:
/foo%28bar您不需要与%匹配,因为URI被解码为:
You don't need to match against %, because the URI gets decoded into:
/foo(bar,您需要与(进行匹配。更好的选择可能是与每个除外点匹配:
and you need to match against (. A better option may to just match against every except dots:
RewriteRule ^([^.]+) search.php?id=$1&ff=$2&ffid=$3或您比赛中不想要的任何内容。
or whatever you don't want in your match.
尝试:
RewriteRule ^([^.]+)$ search.php?id=$1 [B]这里的区别是 $ 将匹配项绑定到URI的末尾,而 B 标志可确保& 得到编码。
The difference here is the $ to bound the match to the end of the URI, and the B flag ensures the & gets encoded.
更多推荐
如何处理&等特殊字符和/.htaccess规则中?
发布评论