Matching Digits in a mod_rewrite RewriteRule

Album Cover: Life in 1472

"Even with a patch on my eye, I'm dreamy."
Slick Rick / Fresh

Posted on January 15, 2008 1:49 PM in Web Development
Warning: This blog entry was written two or more years ago. Therefore, it may contain broken links, out-dated or misleading content, or information that is just plain wrong. Please read on with caution.

If you read up on the syntax of RewriteRule in Apache's mod_rewrite support, you may be led to believe that the full extent of Perl Regular Expressions is supported. After all, the following shows up verbatim in the RewriteRule documentation:

For more information about regular expressions, have a look at the perl regular expression manpage ("perldoc perlre").

However, I've found that it just isn't so. In an attempt to match a string of digits in a request and redirect to a page when the condition is met, I did something like the following:

RewriteRule ^\d\d\d /flames/ [L]

If working properly, a request to http://www.example.com/666 would redirect to http://www.example.com/flames/. However, it wasn't doing what I wanted to. I even tried using ^\d{3} as my pattern, but no dice.

As it turns out, it appears that mod_rewrite prefers character sets in RewriteRule patterns. Therefore, the following works as expected:

RewriteRule ^[0-9]{3} /flames/ [L]

So if you're trying to match numbers/digits in a RewriteRule, be sure to use a character set instead of the \d digit shortcut so you can avoid wasting half-an-hour of your time like I just did.

Comments

Jemima on January 25, 2008 at 5:51 AM:

Thank you! You just saved me half an hour :-)

Permalink

Darwin on April 20, 2009 at 2:26 PM:

thanks...no wonder it wasn't working. stupid mod_rewrite.

Permalink

Zen of Linux on January 30, 2010 at 2:25 PM:

Thanks a lot!

Permalink

Sane on February 20, 2011 at 2:44 PM:

Worked like a charm. Thanks

Permalink

followers on August 31, 2016 at 12:52 AM:

Much obliged for an awesome offer. Your article has demonstrated your diligent work and experience you have in this field. Splendid .i cherish it perusing.

Permalink

instalikes on August 31, 2016 at 12:52 AM:

I discovered your this post while looking for some related data on online journal search...Its a decent post..keep posting and overhaul the data.

Permalink

get followers on instagram on September 03, 2016 at 11:58 PM:

Great article with excellent idea!Thank you for such a valuable article. I really appreciate for this great information..

Permalink

get likes on September 03, 2016 at 11:58 PM:

Great article with excellent idea!Thank you for such a valuable article. I really appreciate for this great information..
get followers on instagram|get likes

Permalink

jrcofvvk on May 15, 2017 at 5:23 AM:

Post Comments

If you feel like commenting on the above item, use the form below. Your email address will be used for personal contact reasons only, and will not be shown on this website.

Name:

Email Address:

Website:

Comments:

Check this box if you hate spam.