0

I made a test on my home computer using 127.0.0.1 as the IP address. I made some rewrite rules with a ruleset as follows:

RewriteEngine On
RewriteRule ^x\/x$ /letterx [NC,L]
RewriteRule ^y/y$ /lettery [NC,L]

When I go in my browser and enter 127.0.0.1/x/x, I get redirected to 127.0.0.1/letterx. When I enter 127.0.0.1/y/y, I get redirected to 127.0.0.1/lettery.

Looking online at various sources of regular expressions, no article claims that / is a character that needs escaping, but http://regex101.com/ claims it does.

Why in this set of code do both urls I try redirect successfully? Shouldn't one throw a failure? Is there a bug in mod_rewrite I'm not aware of? You would think based on many websites that the second line of code above should fail and the third line should work, but I'm puzzled.

  • @Prix you are right about PCRE, but `/` is a not a character of special meaning and does not need to be escaped. The only time it needs to be escaped is if the regex delimiters are set to `/` ... – hwnd Nov 12 '14 at 00:30
  • http://stackoverflow.com/questions/3591452/do-you-have-to-escape-a-forward-slash-when-using-mod-rewrite – hwnd Nov 12 '14 at 00:33
  • @hwnd yes, it does not need to be escaped because of whatever method mod_rewrite uses by either changing the default modifier or pre-escaping the `/` for security or whatever reason. ***However the way you worded your answer was not right and gave a wrong feel of what the `/` was/stand for in regards the PCRE.*** – Prix Nov 12 '14 at 00:33
  • Is there a configuration file to use to change those regex delimiters? –  Nov 12 '14 at 00:34
  • @Mike not that I am aware of but you can safely assume that `/` does not require escaping in `mod_rewrite` nor `.htaccess` in most if not all cases where its used along with `mod_rewrite` or `.htaccess`. – Prix Nov 12 '14 at 00:35
  • what are the cases where / must be delimited? I'm using advanced rewriting rules –  Nov 12 '14 at 00:37
  • @Mike I've answered countless questions about mod_rewrite and I haven't came across any that I had to do that for as of yet but perhaps some other member had and will enlighten you on that. – Prix Nov 12 '14 at 00:39

1 Answers1

0

It looks like by default, the regex101 site uses PCRE style regular expressions, which is used by php, and are delimited by two / characters. Example:

/thisismyregex/

thisismyregex is the regular expression surrounded by / delimiters, and if I want to specify regex flags, I add them after the last /:

/thisiscaseinsensitive/i

Because PCRE style regex uses / as delimiters, if you want to match a / inside your regex, you need to escape it:

/this\/is\/my\/regex/i

which will case insensitive match: this/is/my/regex

However, mod_rewrite does not use PCRE style regex delimiters, so you don't need to bother escaping the /'s. Example on the regex101 site, switch the regex style to something else, either javascript or python, and you'll notice that you suddenly don't need to escape the /'s anymore.

Is there a configuration file to use to change those regex delimiters?

No. But you can look at the apache documentation for regex.

Jon Lin
  • 142,182
  • 29
  • 220
  • 220