【问题标题】:IIS 8 URL Rewrite Module for redirecting Non-WWW to WWW (except URLs with sub-domains)用于将非 WWW 重定向到 WWW 的 IIS 8 URL 重写模块(带有子域的 URL 除外)
【发布时间】:2018-08-26 05:27:04
【问题描述】:

我正在将我所有网站的非 www 版本的域重定向到 www 版本。

但是,副作用是它还使用 www 重定向子域

例如api.example.com 到 www.api.example.com

这不是我想要的。我需要所有带有子域的网站都不要添加 www。

这是我目前的规则:

<system.webServer>
    <rewrite>
        <globalRules>
            <clear />
            <rule name="non-www to www" enabled="true" stopProcessing="false">
                <match url=".*" />
                <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                    <add input="{CACHE_URL}" pattern="^(.+)://(?!www)(.*)" />
                </conditions>
                <action type="Redirect" url="{C:1}://www.{C:2}" />
            </rule>     

            <rule name="http to https" stopProcessing="true">
                <match url="(.*)" /> <!-- Require SSL must be OFF in the site settings -->
                <conditions>
                <add input="{HTTPS}" pattern="off" ignoreCase="true" />
                </conditions>
                <action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}{REQUEST_URI}" />
            </rule>
        </globalRules>
    </rewrite>
</system.webServer>

如果输入不以“www”开头并且它不是子域,是否可以制定仅重定向到 www.example.com 的规则?

任何带有子域的东西都不能在 www

请帮忙,因为我已经尝试了两天没有成功。

提前谢谢你!!

【问题讨论】:

    标签: iis subdomain iis-7.5 iis-8 url-rewrite-module


    【解决方案1】:

    您需要将您的正则表达式更改为:^[^.]*\.[^.]{2,3}(?:\.[^.]{2,3})?$,此正则表达式将仅捕获二级域。

    我从这个答案中得到它:https://stackoverflow.com/a/21174423/1954204

    最后你的规则是这样的:

    <rule name="non-www to www and https" enabled="true" stopProcessing="false">
        <match url=".*" />
        <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
            <add input="{HTTP_HOST}" pattern="^[^.]*\.[^.]{2,3}(?:\.[^.]{2,3})?$" />
        </conditions>
        <action type="Redirect" url="https://www.{HTTP_HOST}" />
    </rule>     
    

    【讨论】:

    • 像魅力一样工作!谢谢大家!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-09
    • 2011-06-24
    • 2018-07-25
    • 2015-08-03
    • 1970-01-01
    • 2019-03-09
    • 2011-11-05
    相关资源
    最近更新 更多