4

But stuck on this one, looked on google but couldnt find anything.

The deployment of the site went slightly wrong and some pages were being saved as:

http://73.34.12.../page.aspx

Where the IP was the underlying IP address for the domain (so the pages served fine).

Now however, lots of crawlers are indexing the IP site, AND the main site. This is a waste of bandwidth and causes some duplicate content issues!

How can I redirect the IP to the domain?

BenGC
  • 1,775
  • 15
  • 26
Tom Gullen
  • 375
  • 4
  • 7
  • 24

1 Answers1

9

When site is accessed by IP the HTTP_HOST will be an IP address (or maybe just blank -- I have tested this on my PC and is was an IP address). If so -- then you can use simple URL Rewrite rule to do a 301 redirect to a proper domain name.

Here is an example of such web.config (when HTTP_HOST is an IP). You need URL Rewrite module to be installed (v1 is already bundled with IIS 7.5, but you may want to upgrade to v2). Works fine locally on Windows 7:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="IP Hit" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions>
                        <add input="{HTTP_HOST}" pattern="192.168.0.3" />
                    </conditions>
                    <action type="Redirect" url="http://www.example.com/{R:1}" redirectType="Permanent" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

P.S. You would need to change my local IP address to the one you have got on server (73.34.12...)

LazyOne
  • 3,014
  • 1
  • 16
  • 15
  • @LazyOne This works perfectly. Does it matter if the browser sends the IP address or a blank entry in the request header? I've read that some browsers send the IP others just a blank entry. – Jacques Jun 30 '16 at 13:42
  • 1
    @Jacques Well ... the rewrite rule I have provided in my answer works for IP. You may create similar but for empty value. But in general -- my local tests show that the browsers that I have used send IP in that field. – LazyOne Jun 30 '16 at 19:27
  • this doesnt work for `https://192.168.0.3/` any idea how to make it work? – Vaibhav Garg Feb 28 '19 at 05:34
  • @VaibhavGarg What do you mean by "does not work"? Of course it works ... but only after HTTPS connection has been fully established (valid certificate present etc) as URL rewrite works AFTER that stage, not before. – LazyOne Feb 28 '19 at 09:02
  • I get this error `Your connection is not private Attackers might be trying to steal your information from 192.168.0.3` The certificate is for the domain and does not contain IP . I created a question here https://stackoverflow.com/posts/comments/96618125 – Vaibhav Garg Mar 01 '19 at 08:59
  • @VaibhavGarg That message comes from the browser, that checks all that info before fully establishing the connection. It's out of your control. The only way is to either add an IP into certificate or somehow whitelist that exception in the browser in advance. – LazyOne Mar 01 '19 at 09:13
  • If you need to specify a generic IP address rather than a specific one, you can use \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} as the pattern value for the HTTP_HOST input. This will redirect for any IP address sent as the host name. Useful for NAT or farm instances. – Prof Von Lemongargle Sep 18 '19 at 21:53