将所有裸域名的URL重定向到子域名(www)URL,保留URL,但在IIS/ASP.NET上除了一个页面。

5

如何实现上述目标最佳方式是什么?我知道可以在HttpModule级别实现。是否可能仅通过web.config实现(更容易和更快地编写执行)。

1个回答

6

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

请参考以下 Stack Overflow 问题:IIS7 URL 重写:如何在重写的 URL 中不丢失 HTTPS 协议?,以处理 HTTPS/HTTP 重定向。 - Bart Verkoeijen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接