IIS7.5 URL Rewrite rule to perform a 301 redirect from thesite.hosting.com to thesite.co.uk

iisiis-7iis-7.5url-rewriting

I use IIS 7.5

I have a website wich has a valid host like:

A) mysite.co.uk

and a DEFAULT host (using for testing proposes provided by the hosting company):

B) mysite.hosting.com

Website is visible on both address, creating a DUPLICATE CONTENT issue for Search Engine.

I need redirect all the traffic (for all pages) from B to A using a 301 redirect.

IIS7.5 Http Redirect it is not design for this situation so I suppose to use IIS 7.5 Url Rewrite Module.

My questions: how write the ROLE in my web.config? Thanks

Best Answer

Try adding something like this between the <System.webServer> tags in your web.config:

<rewrite>
    <rules>
        <rule name="Redirect mysite.hosting.com to mysite.co.uk" patternSyntax="Wildcard" stopProcessing="true">
            <match url="*" />
            <conditions>
                <add input="{HTTP_HOST}" pattern="mysite.hosting.com" />
            </conditions>
            <action type="Redirect" url="http://mysite.co.uk/{R:0}" />
        </rule>
    </rules>
</rewrite>

Alternatively, you can do this using global rules by adding:

<rewrite>
    <globalRules>
            <rule name="Redirects to mysite.co.uk" patternSyntax="ECMAScript" stopProcessing="true">
            <match url=".*" />
            <conditions logicalGrouping="MatchAny">
                <add input="{HTTP_HOST}" pattern="mysite.hosting.com$" />
            </conditions>
            <action type="Redirect" url="http://mysite.co.uk/{R:0}" />
        </rule>
    </globalRules>
</rewrite>
Related Topic