使用 mod_rewrite 时是否必须转义正斜杠? [英] Do you have to escape a forward slash when using mod_rewrite?

查看:17
本文介绍了使用 mod_rewrite 时是否必须转义正斜杠?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于向 RewriteRule 或 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.

如果您想知道这段代码的意义,它会将城市、县和地区(带或不带正斜杠)重定向到它们的复数形式.此外,如果复数有一个正斜杠,它会删除正斜杠.

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.

推荐答案

不,您不必转义斜杠.正斜杠在正则表达式中没有任何特殊含义.

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.

这篇关于使用 mod_rewrite 时是否必须转义正斜杠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆