【问题标题】:url rewrite -> for non www to www - which is better and how? (site level or application level)url 重写-> 对于非 www 到 www-哪个更好,如何? (站点级别或应用程序级别)
【发布时间】:2011-11-07 15:13:47
【问题描述】:

我正在为一个基于 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 中的 url 重写。

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

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

【问题讨论】:

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


    【解决方案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>
    

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

    【讨论】:

      猜你喜欢
      • 2016-07-28
      • 2011-03-20
      • 1970-01-01
      • 2015-02-27
      • 2012-04-18
      • 2015-11-06
      • 2017-05-03
      • 2014-12-16
      • 1970-01-01
      相关资源
      最近更新 更多