4

I have a website with 2 different domains, for example:

www.example.com www.example.net

Now i want, that every user coming from example.com should be redirected to www.example.de

Ill tried:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^example\.com$
RewriteRule ^.*$ http://example.de/$0 [L,QSA,R=301]

But now still all users from example.net get redirected to example.de

How can i solve that, that only users from example.com gets redirected (also with all subfolders).

Thanks!

derdida
  • 14,784
  • 16
  • 90
  • 139

1 Answers1

7

Try this - you want to match example.com (remove the !), and it's clearer to capture the incoming url into $1.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^(.*)$ http://example.de/$1 [L,QSA,R=301]

Also, while debugging, change the R=301 to R, so your browser doesn't "stick" with an old rule. When it works, change it back to R=301

goodeye
  • 2,389
  • 6
  • 35
  • 68
  • Thanks - but its still not working. When ill try example.net it is still redirecting - ill checked already if there are others rules, but there is nothing here. Its a simple hosting provider, so i dont know how he handles the multiple domains. Any other ideas? – derdida Jun 30 '15 at 18:40
  • Try browser in incognito mode, or clear history/cache. There's a chance the browser cached the original redirect (Chrome does this a lot). – goodeye Jun 30 '15 at 18:43
  • Oh ok, it seems that the browser already cached the redirect. Ill try it now again with another browser – derdida Jun 30 '15 at 18:44
  • Thanks - now tried again with deleting the browser cache. When ill remove the "!" its not working (so there is no redirect) - maybe you have another idea? – derdida Jun 30 '15 at 18:51
  • If these rules are on the example.com website, it should definitely be redirecting. (These rules don't do www.example.com, just example.com). As @anubhava said in his comment, removing the ! should have worked (even without my other changes). Does it do anything at all, or does it stay on example.com? – goodeye Jun 30 '15 at 18:53
  • Ah you are right, its redirecting from example.com well (not with www.) - but still the problem that both urls get redirected :( – derdida Jun 30 '15 at 18:57
  • Then it still might be the browser thing - it's hard to get rid of it. Try incognito mode. If that still redirects, it may be something with how the host is set up, so you may have to talk to the provider (e.g,. if they're somehow mapping example.net to example.com, so it's coming through with HTTP_HOST as .com for both - just guessing). – goodeye Jun 30 '15 at 18:59
  • Ok thanks for your help! I will ask the provider here. Maybe you can tell me how to get www.example.com and example.com redirected? – derdida Jun 30 '15 at 19:00
  • Easiet way is remove the `^` from the front of example.com. There are other posts if you want to make it more specific to www. – goodeye Jun 30 '15 at 19:02
  • Thank you so much for your support!! – derdida Jun 30 '15 at 19:03
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/82008/discussion-between-goodeye-and-derdida). – goodeye Jun 30 '15 at 19:11
  • 1
    @goodeye your tip about changing R=301 to R is brilliant mate. That has been giving me so much trouble – garek007 May 21 '19 at 17:01