【问题标题】:Rewrite on IIS7 not working correctly with "www"在 IIS7 上重写无法使用“www”正常工作
【发布时间】:2012-11-13 19:27:21
【问题描述】:

我的重定向规则有问题。我希望我的页面有“www”前缀。它适用于某些页面,而在其他页面上它根本不做任何事情。这是我的规则:

    <rule name="WWW Rewrite" enabled="true">
      <match url="(.*)" />
      <conditions>
        <add input="{HTTP_HOST}" negate="true"
          pattern="^www\.([.a-zA-Z0-9]+)$" />
      </conditions>
      <action type="Redirect" url="http://www.{HTTP_HOST}/{R:0}"
        appendQueryString="true" redirectType="Permanent" />
    </rule>

任何帮助将不胜感激!

【问题讨论】:

  • 我错了说你需要在 Global.asax 文件中实例化它吗?

标签: asp.net redirect iis-7 global-asax url-rewrite-module


【解决方案1】:

这是您的规则,您只需检查您的主机是否与预期主机不匹配,然后重定向到正确的 url。

  <rules>
    <rule name="Canonical Host Name" stopProcessing="true">
      <match url="(.*)" />
      <conditions>
        <add input="{HTTP_HOST}" negate="true" pattern="^www\.domain\.com$" />
      </conditions>
      <action type="Redirect" url="http://www.domain.com/{R:1}" redirectType="Permanent" />
    </rule>
  </rules>

【讨论】:

    【解决方案2】:

    你可以试试这个^(www.)?([.a-zA-Z0-9]+)$

    如果页面当前没有“www”,则匹配。并且应该匹配是否匹配。

    您需要将操作编辑为此以选择第二组 <action type="Redirect" url="http://www.{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />

    【讨论】:

    • 不,它工作得不太好。我认为我没有给出所有的输入。我的网站是 https 或 http。因此,当用户尝试导航到site.com 时,它应该转到wwww.site.com,如果用户尝试导航到site.com,它应该转到site.com ... **site.com 只是一个示例。
    • 你如何决定哪些是要访问site.com,哪些是访问www.site.com?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-06
    • 2019-10-16
    • 2020-09-25
    相关资源
    最近更新 更多