Welcome to WebmasterWorld Guest from 54.144.68.27

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Underscore in character class causes error

Seems okay to me, though

     

Patrick Taylor

9:06 pm on Apr 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



When I use:

RewriteRule ^([A-Za-z0-9-]+)$ /cms/$1.php [L]

... the .htaccess file is fine, but when I add an underscore between the square brackets:

RewriteRule ^([A-Za-z0-9-_]+)$ /cms/$1.php [L]

... I receive an Internal Server Error.

This only happens on one particular server (I think). Should this not work either way?

Patrick

Patrick Taylor

9:39 pm on Apr 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I should add that reversing the order of the dash and underscore like so:

RewriteRule ^([A-Za-z0-9_-]+)$ /cms/$1.php [L]

... doesn't cause an error.

Patrick

jdMorgan

2:13 pm on Apr 9, 2008 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



It is the hyphen causing the problem, not the underscore.

A-Z is interpreted as "any uppercase character from A to Z," then "a-z" is interpreted as "any lowercase character from a to z," then "0-9" as "any numeric character," and then the parser hits "_-]" and tries to interpret that as "any character, from underscore to right square bracket" which of course is invalid (because right square bracket is a lower character-code value than underscore).

And even if that did work, then the character-group would be left unclosed since the right square bracket has already been parsed and "consumed."

I recommend escaping the hyphen when not intended as a "range indicator" -- That is, use "\-" to avoid all ambiguity.

Also, see the mod_rewrite [NC] flag, which makes character-matching case-insensitive. In many cases, it can be used to shorten [A-Za-z] to just [A-Z] or [a-z], and eliminates an entire range-comparison step.

Jim

Patrick Taylor

9:46 pm on Apr 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Many thanks. I'll escape the hyphen to be safe, but in fact the following (with the hyphen after the underscore):

RewriteRule ^([A-Za-z0-9_-]+)$ /cms/$1.php [L]

... didn't cause an error. The error was caused by the underscore after the hyphen:

RewriteRule ^([A-Za-z0-9-_]+)$ /cms/$1.php [L]

Point taken re the [NC] flag.

Patrick

 

Featured Threads

Hot Threads This Week

Hot Threads This Month