0

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.

3
  • 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, 2012 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, 2012 at 21:19
  • Also, to confirm, yes I have been testing with both a / on the end and without.
    – Rick
    Jul 6, 2012 at 23:24

1 Answer 1

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]

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .