3

Somehow, Google has indexed my site via IP address as well as by domain. E.g., 180.10.1.1/index.php as well as www.mysite.com/index.php

I want to 301 all those ip address urls to the appropriate hostname urls, but can't figure out how to do it in nginx.conf.

Thank you for your help...

1
  • Neither of these answers work for me with the latest version of nginx. I just end up in an infinite redirect loop. I've even tried to regex the IP hoping it would stop future requests but to no avail.
    – user100531
    Nov 9, 2011 at 23:14

3 Answers 3

8

Add another server block to your config file

server {
    listen 180.10.1.1:80;
    server_name 180.10.1.1;
    rewrite .* http://www.mysite.com$request_uri permanent;
}
0
2

if you have latest version of nginx:

server {
  listen 80 default;
  rewrite ^ http://mysite.com$request_uri permanent;
}
1
  • in this case default means bind to all ips 0.0.0.0 i think, which might give you unexpected results?
    – djangofan
    Nov 9, 2011 at 23:23
1

The above 2 answers didn't work for me either and resulted in an infinite redirect loop. Adding the ip address to my server_name worked:

server {
    listen 80;
    server_name mydomain.com www.mydomain.com 67.20x.xxx.xx;
       ...
    }

You must log in to answer this question.