homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

Underscore in character class causes error
Seems okay to me, though
Patrick Taylor

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

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 Taylor

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

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.



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

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.


Patrick Taylor

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

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.


Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved