Forum Moderators: phranque

Message Too Old, No Replies

Rewrite rule working in one domain but not in another

rewrite rule problem

         

anees_muhd

4:45 am on Nov 19, 2008 (gmt 0)

10+ Year Member



Hi

i have written a rewrite rule to redirect into a PHP page.

RewriteRule ^Details/([0-9]+)/([a-zA-Z0-9-,]+)$ category\.php?cat=$1&perma=$2 [L]

it was working like
when i call
example.com/Details/2/Commercial
then it redirects to
example.com/category.php?cat=2&perma=Commercial
with my old hosting

but the same code is not working in my new hosting

the other rules are working as normally in the new hosting
for eg.:

RewriteRule ^full_details$ all_category\.php [L]

Prev. Apache version2.2.9 (Unix)
Present Apache/1.3.37

Please help
Thanks in Advance
Regards
Anees

anees_muhd

6:41 am on Nov 19, 2008 (gmt 0)

10+ Year Member



The problem has been solved now
i just played around and changed the order in this
([a-zA-Z0-9-,]+)
the symbol "-" i placed at last
([a-zA-Z0-9,-]+)
and it works fine

But can someone help me that what exactly happened here

jdMorgan

3:50 pm on Nov 19, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The hyphen "-" is used as a "range indicator" within alternate-character [groups]. Therefore "9-" was interpreted as a character-range starting with "9", but missing an end-of-range character. Because "," is not a valid start-of-range character, putting the hyphen last solved the problem.

A more robust solution is to escape the "-" character by preceding it with a "\" whenever it is used in a character-group. This allows you to put the hyphen in any position within the group, so that characters can be ordered from most-likely-to-occur to least-likely-to-occur for the sake of efficiency.

Note that the escaping rules inside alternate-character groups are more relaxed than outside alternate-character groups. With a character group, only "^", "-", and "]" need to be escaped. Outside alternate-character groups, the following characters must be escaped in mod_rewrite regex patterns if you wish to match them as literals, rather than use them as regex pattern tokens or operators:

"^", "$", "%", "?", "*", "+", "(", ")", "[", "]", "{", "}", "¦", "\", ".", in all cases, and ' " ', ">", "<" or "=" if used as the initial character of the pattern.

Jim

anees_muhd

1:20 pm on Nov 20, 2008 (gmt 0)

10+ Year Member



Hi
Thanks Jim for ur Reply

But still it was working fine in Apache version2.2.9

Anees

jdMorgan

4:08 pm on Nov 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The regex parsing rules may have been slightly changed, as that is the only sensible explanation. The older version recognized the third hyphen before it recognized the second one, which is sort of strange. The state machine that interprets ranges would be expected to say, "OK, we just finished the 0-to-9 range, and the next character is also a hyphen, so this cannot be another range, and so I interpret this third hyphen as a literal character."

This is what apparently does happen on Apache 2.x, but it seems that the older 1.3 version got confused and tried to treat it as another range declaration overlapping with the 0-9 range; It thought you were trying to define two ranges, both "0"-to-"9" and "9"-to-"," and it got confused.

Using [a-zA-Z0-9\-,] with the third hyphen escaped by the preceding "\" is a safe and robust solution that will work an any server and any version of the POSIX regular-expressions library (which is actually part of the operating system, not the server).

Jim