MarvinLazer
MarvinLazer

Reputation: 318

.htaccess redirect specific domain to specific page

I want any traffic coming from urlnumberone.com and urlnumbertwo.com to redirect to a couple of specific pages on my site.

RewriteEngine on
RewriteCond %{HTTP_REFERER} ^http://(www.\.)?urlnumberone\.com
RewriteRule ^$ /thepath/tomypage/goeshere/ [L]

RewriteEngine on
RewriteCond %{HTTP_REFERER} ^http://(www.\.)?urlnumbertwo\.com
RewriteRule ^$ /thesecond/pathgoeshere/ [L]

Right now, both URLs are simply taking the user to the homepage on my site, rather than going to the pages I've intended. What am I doing wrong? In case it matters, the site is a WordPress site and the WordPress rules are UNDER these redirects, not above them in the code.

Upvotes: 1

Views: 2941

Answers (1)

Matt
Matt

Reputation: 148

I think there are two issues. There is an extra dot in the RewriteCond lines, so the portion in parentheses currently means "www(any-character)(literal-dot)", which would prevent matching the www version of the domain that you need to match -- unless you're trying to match domains like www6.urlnumberone.com and www3.urlnumberone.com.

So, I would replace:

RewriteCond %{HTTP_REFERER} ^http://(www.\.)?urlnumberone\.com

With:

RewriteCond %{HTTP_REFERER} ^http://(www\.)?urlnumberone\.com

Next, for the RewriteRule line, I would change the ^$ (replacing an empty string) with .* (replacing the whole path), and use a 302 or 301 redirect. Since WordPress rewrites every URL that isn't an existing file or directory, it's easy to make redirect loops by mistake, so using a 301 or 302 should help prevent that. So I would use:

RewriteRule .* /thepath/tomypage/goeshere/ [R=302,L]

There may be mod_rewrite wizards out there that have a better answer, but this method works well in my experience.

Upvotes: 2

Related Questions