0

There are people who like to mirror sites by pointing domain probably and waste the bandwidth from the server, so I want to redirect mirrored users to the real one

I tried with...

if ($_SERVER['SERVER_NAME'] != 'www.example.com')
{
  header('Location: http://www.example.com');
  exit;
}

...but the problem is that some people use str_replace to fight this.

Then www.example.com part becomes same as their mirror site, hence it's the same as $_SERVER['SERVER_NAME'] and it won't work.

Shadow
  • 337
  • 1
  • 2
  • 6
  • If you set up the web server properly, it should pass through requests that has the hostnames you've configured. Then those request won't even reach your PHP-application.. How to do that depends on the environment and what web server you're using. – M. Eriksson Dec 04 '19 at 10:18
  • I have setup nginx to listen only to cloudflare IP addresses and blocked everything else via iptables – Shadow Dec 04 '19 at 10:21
  • Sure, but you should also configure [server_name](http://nginx.org/en/docs/http/server_names.html) with the hostnames you want to allow. Then you can create a vhost without a server name, which will act as a honey-pot for other requests. That can simply return 404 – M. Eriksson Dec 04 '19 at 10:22
  • Exactly like that. `listen custom_port ssl http2; server_name www.example.com;` – Shadow Dec 04 '19 at 10:23
  • Does this answer your question? [Why is nginx responding to any domain name?](https://stackoverflow.com/questions/9824328/why-is-nginx-responding-to-any-domain-name) – M. Eriksson Dec 04 '19 at 10:26
  • ^ That shows how you can do what I suggested in my second comment. – M. Eriksson Dec 04 '19 at 10:27
  • 2
    A site mirror redirecting to the original site kind of loses it's point as a mirror since if the original site is down the mirror will be down as well – apokryfos Dec 04 '19 at 10:27
  • I actually tried with server `server { return 404; }` to the first block, but the mirror is still mirroring it. – Shadow Dec 04 '19 at 10:35

1 Answers1

0

If you're using apache you can add a .htacess file into your web-root folder that does the work for you. The advantage of this is that if you want to test locally you can change that specific file.

Here is a example:

<If "%{HTTP_HOST} != 'www.example.com'">
Redirect / http://www.example.com/
</If>
Nathan Kolpa
  • 89
  • 1
  • 11