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...

  • 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. –  Nov 09 '11 at 23:14

3 Answers3

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;
}
SaveTheRbtz
  • 5,691
  • 4
  • 32
  • 45
2

if you have latest version of nginx:

server {
  listen 80 default;
  rewrite ^ http://mysite.com$request_uri permanent;
}
edogawaconan
  • 311
  • 1
  • 4
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;
       ...
    }
mlinfoot
  • 11
  • 1