3

I have a site with two domains. I want that whenever domain X is visited (no matter what the path is... /foo/bar/, root or whatever), the browser should redirect to a specific URL.

So:

domainX.com -> domainX.com
domainY.com -> domainX.com/some/path

The following kinda sorta works, but it only matches against domainY.com, so www.domainY.com or domainY.com/some/path doesn't work.

RewriteCond %{HTTP_HOST} ^domainY\.com
RewriteRule ^(.*)$ http://domainX\.com/some/path [L]

It has to accept both with and without www before though. Any ideas?

qwerty
  • 5,166
  • 17
  • 56
  • 77

1 Answers1

6

You can extend the regular expression for HTTP_HOST

RewriteCond %{HTTP_HOST} ^(?:www\.)?domainY\.com$
RewriteRule .* http://domainX.com/some/path [L]

This is a rewrite. If you want to redirect the client, you must add an R flag

RewriteRule .* http://domainX.com/some/path [R,L]

When everything works as it should, you may replace R with R=301. Never test with R=301.

Community
  • 1
  • 1
Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
  • Now www also works, but if i put something after the domain (ex `domainY.com/some/path` it doesn't work. That's not as important though, i can live without it. Thank you! – qwerty Mar 05 '13 at 09:59
  • @qwerty This is strange, it should work with or without an additional URL-path. Do you have additional rules? Or do you get some error message? – Olaf Dietsche Mar 05 '13 at 10:17
  • I do have additional rules. They were in the OP but i edited them out because i thought it was irrelevant. See here: http://pastebin.com/Xh0u1AsA Now that i think about it, i should also probably put my rules within the . Could the other rules cause it to not work? – qwerty Mar 05 '13 at 10:22