Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am new to using regular expressions for rewriting URL's in htaccess

I need to redirect to, IF cookie named 'ref' is set.

my current htaccess is:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_COOKIE} ref=true [NC]
RewriteRule ^/([0-9]+)/$

The goal is that when someone enters site with: number) that they are redirected to the home page of the site after the cookie is set.

share|improve this question
That expression is going to match /123/ (note the trailing slash) -- are you testing with this? Does the redirect happen if you comment out the RewriteCond? – mgorven Jul 6 '12 at 20:15
If I comment out the RewriteCond nothing happens, no redirect with or etc. – Rick Jul 6 '12 at 21:19
Also, to confirm, yes I have been testing with both a / on the end and without. – Rick Jul 6 '12 at 23:24

That rule will never match, since you're both in an htaccess file and have set a RewriteBase. Both of those factors set a relative context that RewriteRule functions within - so it drops the leading slash on the match string.

Try this:

RewriteRule ^([0-9]+)/?$ [R=302,L]
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.