IIS7.5 redirect IP to domain

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?


Solution 1:

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