Siva Bathula
Siva Bathula

Reputation: 766

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

Upvotes: 5

Views: 3952

Answers (1)

Marco Miltenburg
Marco Miltenburg

Reputation: 6138

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>

Upvotes: 6

Related Questions