Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect all naked domain urls to subdomain(www) urls preserving the url, except for one page on IIS/ASP.NET

Whats the best way to achieve the above? I do know that it can be achieved at HttpModule level. Is it possible just via web.config(easier and faster to code execute).

like image 923
Siva Bathula Avatar asked Nov 02 '12 12:11

Siva Bathula


1 Answers

It's easy to do this with the URL rewrite module through the web.config :

<rewrite>
    <rules>
        <clear />
        <rule name="Redirect naked domains to www.domain.com" stopProcessing="true">
            <match url="(.*)" />
            <conditions logicalGrouping="MatchAll">
                <add input="{HTTP_HOST}" negate="true" pattern="^www\." />
                <add input="{REQUEST_URI}" negate="true" pattern="^noredirect/forthis/page\.aspx$" />
                <add input="{REQUEST_URI}" negate="true" pattern="^noredirect/forthis/page-as-well\.aspx$" />
                <add input="{REQUEST_URI}" negate="true" pattern="^noredirect/forthis/page-as-well-too\.aspx$" />
            </conditions>
            <action type="Redirect" url="http://www.{HTTP_HOST}{REQUEST_URI}" appendQueryString="false" redirectType="Permanent" />
        </rule>
    </rules>
</rewrite>

Or if you really only have a single page that doesn't need to be redirected, it can be even shortened to:

<rewrite>
    <rules>
        <clear />
        <rule name="Redirect naked domains to www.domain.com" stopProcessing="true">
            <match url="^noredirect/forthis/page\.aspx$" negate="true" />
            <conditions logicalGrouping="MatchAll">
                <add input="{HTTP_HOST}" negate="true" pattern="^www\." />
            </conditions>
            <action type="Redirect" url="http://www.{HTTP_HOST}{REQUEST_URI}" appendQueryString="false" redirectType="Permanent" />
        </rule>
    </rules>
</rewrite>
like image 179
Marco Miltenburg Avatar answered Oct 12 '22 11:10

Marco Miltenburg