Search code examples
asp.netasp.net-mvc-4iishttp-redirecthttp-status-code-301

Redirecting non - www urls to www


Currently both http://example.com and http://www.example.com works fine. However we need to make all http://example.com urls to be redirected to http://www.example.com.

The site is an ASP.net MVC4 website. To get it done I have added the following web.config entry under System.webserver.

<httpRedirect enabled="true" destination="http://www.example.com" />

However it is resulted in a redirect loop. Can you please help me get this done.


Solution

  • Try Adding this to web.config. There must be rewrite module installed in your IIS. Read this Article for better explanation.

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