Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect *.domain.com & domain.com to www.domain.com

I've got an Search Engine Optimisation problem where users are able to access my site by specifying any sub-domain. This is causing duplicate page issues with SEO.

For example if a user mis-types 'www' then posts a link on a forum, google is crawling 'wwww.domain.com'. Furthermore, google is also crawling 'domain.com'.

I need a way of forcing the site to always redirect to 'www.domain.com' regardless of how the user accesses the site.

Is it possible to do this in the web.config file? If not, how else can I achieve this?

Cheers, Curt.

like image 509
Curtis Avatar asked Dec 23 '22 00:12

Curtis


1 Answers

You can do this using the IIS URL Rewrite module. Here's the config to rewrite domain.com to www.domain.com:

<system.webServer>
    <rewrite>
        <rules>
            <rule name="Canonical host name">
                <match url="^(.*)" />
                <conditions>
                    <add input="{HTTP_HOST}" pattern="^www\.domain\.com$" negate="true" />
                </conditions>
                <action type="Redirect" url="http://www.domain.com/{R:1}" />
            </rule>
        </rules>
    </rewrite>
</system.webServer>

You might need to modify the regex a bit or add a second rule to support rewrite of specific subdomains as well.

like image 151
Franci Penov Avatar answered Jan 09 '23 03:01

Franci Penov