【问题标题】:Redirect all naked domain urls to subdomain(www) urls preserving the url, except for one page on IIS/ASP.NET将所有裸域 url 重定向到保留 url 的子域 (www) url,IIS/ASP.NET 上的一页除外
【发布时间】:2012-11-02 12:31:27
【问题描述】:

实现上述目标的最佳方法是什么?我知道它可以在 HttpModule 级别实现。是否可以仅通过 web.config(代码执行更容易和更快)。

【问题讨论】:

    标签: iis iis-7.5 asp.net-4.0 httpmodule


    【解决方案1】:

    通过 web.config 使用 URL 重写模块很容易做到这一点:

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

    或者如果你真的只有一个不需要重定向的页面,它甚至可以缩短为:

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

    【讨论】:

    猜你喜欢
    • 2018-08-26
    • 1970-01-01
    • 2013-02-22
    • 1970-01-01
    • 2016-06-10
    • 1970-01-01
    • 2016-05-17
    • 2012-03-10
    • 1970-01-01
    相关资源
    最近更新 更多