[thelist] mod_rewrite regular expression

David Kaufman david at gigawatt.com
Tue Jul 13 11:11:28 CDT 2004


Simon Perry <thelist at si-designs.co.uk> wrote:
> Simon Perry wrote:
>
>> Hi,
>>
>> I am having problems with mod_rewrite using the following;
>>
>> Options +FollowSymlinks
>> RewriteEngine On
>> RewriteBase /
>>
> RewriteRule ^flights/([\w+]+)$ /re_engine/flights.php?area=$1
>>
>> The above should match this
>> www.domain.com/dir/test
>> or this
>> www.domain.com/dir/test_underscore
>> but I end up with a 404 and no messages in the logs.

well, your attempt to rename the directory and script names in this example
(presumably to obscure them from us) make it that much more difficult for us
to help you.  for instance, it's hard to tell you why the directory "dir" or
the file test_underscore would not be found, when your rewrite rule has
nothing to do with these.

in ancy case, try this rule and see if it works for you:

RewriteRule ^flights/([a-zA-Z0-9_]+)$ /re_engine/flights.php?area=$1

iirc, perl's \w "word" metacharacter is not be supported by mod_rewrite's
regex engine.  in which case you have to "spell out" the characters in the
word character class, which are upper and lower letters, all 10 digits, and
the underscore.

also it looks like you have too many +'s in your backreference
(parenthesized part of the) pattern, unless you really did want to allow and
match area name strings with plus signs in them.

the reason you got a 404 from that was that [\w+] was interpreted as a
literal w and the plus sign, making the pattern (almost) never match... it
would only have rewritten URL's such as:
    flights/w+w
to  /re_engine/flights.php?area=w+w

hth,

-dave






More information about the thelist mailing list