Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 881
TrainTC Avatar asked Aug 28 '10 16:08

TrainTC


People also ask

Does forward slash need to be escaped?

What context/language? Some languages use / as the pattern delimiter, so yes, you need to escape it, depending on which language/context. You escape it by putting a backward slash in front of it: \/ For some languages (like PHP) you can use other characters as the delimiter and therefore you don't need to escape it.

How do you escape a slash in path?

About Escaping File Paths Characters to be escaped include the backslash (\, because it is the escaping character) and the double quotes ("). Both of these characters can be relevant in file paths.

What is forward slash in Java?

The forward slash "/" is an ASCII text character that is commonly used to communicate network addresses, URLs, and other addresses on the computer keyboard. The forward slash is used three times in this Javatpoint URL, for example, at https://www.javatpoint.com.

What is mod_rewrite in Apache?

The mod_rewrite module uses a rule-based rewriting engine, based on a PCRE regular-expression parser, to rewrite requested URLs on the fly. By default, mod_rewrite maps a URL to a filesystem path. However, it can also be used to redirect one URL to another URL, or to invoke an internal proxy fetch.


1 Answers

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.

like image 177
John Kugelman Avatar answered Sep 21 '22 16:09

John Kugelman