url rewrite - >对于非www到www - 哪个更好,怎么样? (网站级别或应用程序级别)

时间:2011-11-07 15:13:48

标签: asp.net url-rewriting web-config windows-server-2008-r2

我正在为基于的sigle网站进行网址重写(通配符) this blog post。这就是我试过的:

<rewrite>
    <rules>
        <rule name="Redirect example.com to www" patternSyntax="Wildcard" stopProcessing="true">
            <match url="*" />
            <conditions>
                <add input="{HTTP_HOST}" pattern="example.com" />
            </conditions>
            <action type="Redirect" url="http://www.example.com/{R:0}" />
        </rule>
    </rules>
</rewrite>

这些代码非常完美,我们可以手动将它们添加到网站的web.config或在IIS中使用网址重写。

我的问题是我的IIS上安装了很多网站(域和子域名--net,com,org),我必须为所有这些网站重复工作!

是否可以使用其他方式将非www重定向到所有网站的www(网站级别或应用程序级别)?如果应用程序级别可以更改哪些配置文件?你能告诉我们正确的通配符还是正则表达式?

1 个答案:

答案 0 :(得分:1)

您可以编辑applicationHost.config(在%systemroot%\System32\inetsrv\config目录中),以便它包含IIS安装的常用URL重写规则;以下两个规则(一个用于HTTP,另一个用于HTTPS请求,如果需要)完全符合您的要求:

<system.webServer>
  <rewrite>
    <rules>
      <rule name="NonWwwToWwwRedirect" stopProcessing="true">
        <match url=".*" />
        <conditions>
          <add input="{HTTPS}" pattern="off" />
          <add input="{HTTP_HOST}" pattern="^(?!www\.)(.+)$" />
        </conditions>
        <action type="Redirect" url="http://www.{HTTP_HOST}:{SERVER_PORT}" />
      </rule>
      <rule name="NonWwwToWwwRedirectSecure" stopProcessing="true">
        <match url=".*" />
        <conditions>
          <add input="{HTTPS}" pattern="on" />
          <add input="{HTTP_HOST}" pattern="^(?!www\.)(.+)$" />
        </conditions>
        <action type="Redirect" url="https://www.{HTTP_HOST}:{SERVER_PORT_SECURE}" />
      </rule>
    </rules>
  </rewrite>
</system.webServer>

话虽如此,我认为不可能将一组给定的规则限制在特定的应用程序池中。