Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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

I need to redirect mysite.com/123 to mysite.com/, 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]+)/$ http://www.mysite.com
</IfModule>

The goal is that when someone enters site with: mysite.com/111(some 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 www.mysite.com/1/ or www.mysite.com/1 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

1 Answer 1

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]+)/?$ http://www.mysite.com/ [R=302,L]
share|improve this answer

Your Answer

 
discard

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.