Do you have to escape a forward slash when using mod_rewrite?

核能气质少年 提交于 2019-11-29 11:09:57

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/3591452/do-you-have-to-escape-a-forward-slash-when-using-mod-rewrite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!