IIS7 URL重写 - 添加"www"前缀

nia*_*her 24 xml iis-7 url-rewriting

如何通过IIS7中的URL重写强制example.com重定向到www.example.com?什么样的规则应该进入web.config?谢谢.

Ata*_*har 30

为了使其更通用,您可以使用以下适用于任何域的URL重写规则:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
    <rewrite>
        <rules>
              <rule name="Add WWW" stopProcessing="true">
              <match url="^(.*)$" />
              <conditions>
                 <add input="{HTTP_HOST}" pattern="^(?!www\.)(.*)$" />
              </conditions>
              <action type="Redirect" url="http://www.{C:0}{PATH_INFO}" redirectType="Permanent" />
           </rule>
        </rules>
    </rewrite>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)


ora*_*rad 28

这是Microsoft的URL Rewrite Module 2.0示例,它将*.fabrikam.com重定向到www.fabrikam.com

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Add www" patternSyntax="Wildcard" stopProcessing="true">
                    <match url="*" />
                    <conditions>
                        <add input="{HTTP_HOST}" pattern="www.fabrikam.com" negate="true" />
                    </conditions>
                    <action type="Redirect" url="http://www.fabrikam.com/{R:1}" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>
Run Code Online (Sandbox Code Playgroud)

  • 我想补充一下你可以使用<action type ="Redirect"redirectType ="Permanent"url ="http://www.fabrikam.com/{R:1}"/> (3认同)