你有使用mod_rewrite时逃脱斜线?斜线、你有、mod_rewrite

2023-09-02 09:40:42 作者:﹌泪痕未干′

至于斜杠/给一个正则表达式来重写规则或RewriteCond指令,或其他任何特别的.htaccess相关的时候,有没有必要逃避斜线?

With regards to the forward slash "/" when giving a regex to RewriteRule or RewriteCond, or anything else related to .htaccess in particular, is there a need to escape the forward slash?

下面是什么,我想实现的一个例子

Here is an example of what I am trying to achieve

RewriteEngine on
RewriteOptions inherit

RewriteBase /uk-m-directory/
RewriteRule ^(region|region/|regions/)$ regions [R=301,L]
RewriteRule ^(county|county/|counties/)$ counties [R=301,L]
RewriteRule ^(city|city/|cities/)$ cities [R=301,L]

以上工作正常,并继续正常工作,当我删除反斜杠,如下图所示。

The above works fine, and it continues to work fine when I remove the backslashes as shown below

RewriteEngine on
RewriteOptions inherit

RewriteBase /uk-m-directory/
RewriteRule ^(region|region/|regions/)$ regions [R=301,L]
RewriteRule ^(county|county/|counties/)$ counties [R=301,L]
RewriteRule ^(city|city/|cities/)$ cities [R=301,L]

哪一个是正确的方法?难道他们都错了? 有没有什么特殊的原因正斜杠需要转义,还是不应该?

Which one is the correct way? Are they both wrong? Is there any special reason the forward slash should be escaped, or shouldn't?

我的猜测是,斜线不需要进行转义,因为它不是一个特殊字符,据我所知。但我只是想确定。

My guess is that the forward slash does not need to be escaped because it isn't a special character, as far as I know. But I just want to be sure.

如果你想知道这个code点,它重定向市,县,区(带或不带斜杠),以它们的复数等同。此外,如果多个有正斜杠它消除了正斜杠。

In case you're wondering the point of this code, it redirects city, county, and region (with or without a forward slash) to their plural equivalents. Furthermore if the plural has a forward slash it removes the forward slash.

推荐答案

没有,你不必逃避斜线。斜杠没有在正规前pressions任何特殊含义。

No, you do not have to escape slashes. Forward slashes don't have any special meaning in regular expressions.

这已经咬伤了我在过去的一个共同特点是?在查询字符串。那一个你必须逃脱。

The one common character that has bitten me in the past is ? in query strings. That one you do have to escape.