如何处理&等特殊字符和/.htaccess规则中?

编程入门 行业动态 更新时间:2024-10-23 05:37:46
本文介绍了如何处理&等特殊字符和/.htaccess规则中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在我的.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规则中?

本文发布于:2023-10-29 09:59:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1539398.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何处理   特殊字符   规则   amp   htaccess

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!