Matching Digits in a mod_rewrite RewriteRule

Album Cover: Vitalogy

"Wait for signs, believe in lies, to get by, it's divine."
Pearl Jam / Tremor Christ

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:
Click Here on January 30, 2018 at 2:51 AM:

Much appreciated such a great amount for this data. I need to tell you I agree on a few of the focuses you make here and others might require some further survey, however I can see your perspective.

Permalink

ED treatment on February 23, 2018 at 10:15 PM:

The intellectuals of this field are making the concepts of research very necessary and effective. The production of knowledge is taking place rapidly.

Permalink

landscaping on February 24, 2018 at 6:07 AM:

I think that everything has been described in systematic manner so that reader could get maximum information and learn many things.

Permalink

www.penileguider.com/male-enhancement-pills/xlc-male-enhancement.html on February 27, 2018 at 2:49 AM:

Thank you for some other informative blog. Where else could I get that type of information written in such an ideal means? I have a mission that I’m just now working on. and I have been at the look out for such information.

Permalink

www.penileguider.com/male-enhancement-pills/xlc-male-enhancement.html on February 27, 2018 at 5:09 AM:

Wonderful article. thanks for putting this together! This is obviously one great post. Thanks for the valuable information and insights you have so provided here. www.penileguider.com/male-enhancement-pills/xlc-male-enhancement.html

Permalink

You have a real ability for writing unique content. I like how you think and the way you represent your views in this article. I agree with your way of thinking. Thank you for sharing.http://www.cars-website.co.uk/best-car-polish-reviews/

Permalink

We are ready to serve you anytime-any where live lift your phone, then contact us, we will be ready to come to your place in Hotel, Apartment, or Home.

Permalink

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.