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

依然范特西╮ 提交于 2019-12-10 09:24:38

问题


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


回答1:


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>


来源:https://stackoverflow.com/questions/13195315/redirect-all-naked-domain-urls-to-subdomainwww-urls-preserving-the-url-except

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!