IIS 8 URL重写模块,用于将非WWW重定向到WWW(带有子域的URL除外)

IIS 8 URL重写模块,用于将非WWW重定向到WWW(带有子域的URL除外),iis,subdomain,iis-7.5,iis-8,url-rewrite-module,Iis,Subdomain,Iis 7.5,Iis 8,Url Rewrite Module,我正在将我的所有网站非www版本的域重定向到www版本 然而,它的副作用是,它还通过www重定向子域 e、 g.api.example.com至www.api.example.com 这不是我想要的。我需要所有的子域网站不预装的www 以下是我目前的规则: <system.webServer> <rewrite> <globalRules> <clear /> <rule

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

然而,它的副作用是,它还通过www重定向子域

e、 g.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之前添加

请帮忙,我已经试了两天,但都没有成功


提前谢谢你

您需要将regexp更改为:
^[^.]*\.[^.]{2,3}(?:\.[^.]{2,3})$
,此regexp将只捕获二级域

我从这个答案中得出:

最后,您的规则将是这样的:

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

非常有魅力!谢谢你!