I have a website (mywebsite.com) that was, and partly is, indexed in google. Somebody pointed their own domain (theirsite.com) name to my server and DNS, so it resolves with my IP. Now, probably being an older domain, it outranks me in google, and the pages at my domain are starting to getting de-indexed (probably duplicate content or something). So, for example, my homepage got de-indexed, and their homepage (theirsite.com/) is indexed with my content/code/etc. The same is for other pages (theirsite.com/other/page.html is showing mysite.com/other/page.html)

Quick-fix: To quickly fix it, I have added few lines to my PHP code, checking for $_SERVER['HTTP_HOST'], and if different than my domain, redirects to my domain. It does the job, but to me it looks like a dirty solution.

Question: I could not find a way to have apache to do this job. I would prefer to find an apache/.htaccess solution to this problem (redirecting all traffic from domainA.com/(.*) to domainB.com/$1), is it possible in any way?


1 Answer 1


Your problem looks very similar to preventing hotlinking. There are many tutorials explaining how to do that (here's a random one from Google results), I'd suggest checking them first. They usually are concerned with the referer though, so you'd need to adapt the solution for your case (I believe it's just a matter of replacing HTTP_REFERER with HTTP_HOST). I wish I could help more with the details, but I'm unfamiliar with the workings of mod_rewrite.

In a nutshell, what you need to do for a "proper" solution would be:

  • Create a RewriteCond to match requests from theirsite.com;
  • Create a RewriteRule to do something with those requests (I believe a 410 Gone would be the right action).

Update: after seeing some examples, I believe I was able to get the code right:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^http://(.+\.)?mysite\.com/ [NC]
RewriteRule .*$ - [G]

That would rewrite any request where the HTTP_HOST is not your own site, sending a 410 Gone as the response.

  • Your answer worked perfectly, thanks! That's (nearly) exactely how I wanted to tackle this problem. Now I just want to redirect to my website instead of giving 410 Gone (I dont want to lose the traffic), but I guess I can work that out on my own.
    – milo5b
    Jun 9, 2012 at 14:46
  • For future reference, this is the final apache code: RewriteCond %{HTTP_HOST} !^www.mysite.com$ [NC] RewriteRule ^(.*)$ heichtitipi://www.mysite.com/$1 [R=301,L]
    – milo5b
    Jun 9, 2012 at 14:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.