Forum Moderators: phranque

Message Too Old, No Replies

Characters Needing To Be Escaped in mod rewrite

Do these characters @ ; : ~ need to be escaped in mod_rewrite?

         

MickeyRoush

12:30 pm on Oct 6, 2011 (gmt 0)

10+ Year Member



I'm looking over another webmaster's .htaccess file, and I'm wondering why he escaped these characters? They're used in RewriteCond %{HTTP_USER_AGENT} to block possibly bad user agents.

These are the characters that I don't believe need to be escaped:

@
;
:
~

As far as I know these characters have to be escaped in mod_rewrite:

^$%.*+?!(){}[]|\


And if they don't need to be escaped, what penalty could happen from trying to escape them if any?

wilderness

5:45 pm on Oct 6, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



And if they don't need to be escaped, what penalty could happen from trying to escape them if any?


"Potentially" either this individual rewrite could fail, or any subsequent rewrite lines could fail (same for any syntax error). Possibly could even cause a loop.

On the other side of the coin, I had functioning escaped non-special characters intact for more than an decade.
As a result, there are no firm results, rather, you add the escape and test the results for functionality (same as should be a standard method of practice when making any modification within htaccess).

There's even controversy in Apache in using "any phrase 55" (absent the escapes and included in quotes), while other Apache definitions provide that quotes should be used to surround all Rewrite data.
There's simply to much variation from server-to-server for one-fits-all.

lucy24

7:03 pm on Oct 6, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



And if they don't need to be escaped, what penalty could happen from trying to escape them if any?

Concretely: the backslash could be interpreted as a literal backslash character, meaning that your rule or condition will always fail. Or a negative condition will always match, even when it isn't supposed to.

You can tell when someone has a strong Javascript or php background because there are escaped \; and \/ all over the place ;) Conversely, the one I find most counter-intuitive is that literal spaces inside grouping brackets still have to be escaped. Otherwise you get Bad Things. Possibly even 500 Bad Things. If there's a conflict between an Apache rule (spaces have meaning) and a RegEx rule (brackets have meaning), Apache wins.