本文介绍了如何处理&等特殊字符和/.htaccess规则中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的.htaccess文件中,我定义了以下规则,

In my .htaccess file I have defined following rule,

RewriteRule ^([-0-9a-zA-Z]+) search.php?id=$1

如果我正在浏览
我需要使用符号&网址中的%-/ ,例如:

The above rule works fine if I am browsing http://example.com/abcdI need to use the symbols & % - / in the url like: http://example.com/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规则中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 07:16