【问题标题】:WebConfig URL Rewrites in IISIIS 中的 Web 配置 URL 重写
【发布时间】:2015-06-01 18:16:12
【问题描述】:

拥有 ASP.NET 解决方案并希望实现 2 件事:-

  1. 将 www.mydomain.com 重定向到 mydomain.com
  2. 有友好的网址

所以...这是我添加的 webconfig 的 system.webserver 部分...

 <rewrite>
      <rules>
        <rule name="Remove WWW prefix" >
          <match url="(.*)" ignoreCase="true" />
          <conditions trackAllCaptures="false">
            <add input="{HTTP_HOST}" pattern="^www\.mydomain\.com" />
          </conditions>
          <action type="Redirect" url="{MapProtocol:{HTTPS}}://mydomain.com/{R:1}" redirectType="Permanent" />
        </rule>
        <rule name="homepage" stopProcessing="true" >
          <match url="^/wwwzone/homepage.aspx$"/>
          <action type="Redirect" url="/"/>
        </rule>
      <rule name="homepageReal" stopProcessing="true" >
        <match url="^/$"/>
        <action type="Rewrite" url="/somepath/homepage.aspx"/>
      </rule>
      </rules>
      <rewriteMaps>
        <rewriteMap name="MapProtocol">
          <add key="on" value="https"/>
          <add key="off" value="http"/>
        </rewriteMap>
      </rewriteMaps>
    </rewrite>

这失败了。这背后的逻辑是:-

  • 第一条规则负责将 www.mydomain.com 重定向到 mydomain.com。这行得通。并且使用 rewriteMap 它还可以正确处理 HTTP/HTTPS。
  • 第二条规则应该在浏览器请求不友好的(真实)URL 时进行浏览器重定向。
  • 最后一个旨在将友好 URL 转换回真实 URL,但这是重写而不是重定向。

非常感谢任何想法。

【问题讨论】:

    标签: asp.net url iis url-rewrite-module


    【解决方案1】:

    原来我们的正则表达式语法很差。

    所以,匹配首页的真实网址应该是

    <match url="^$"/>
    

    不是

    <match url="^/$"/>
    

    随后重写为真实 URL 的规则也应该以同样的方式改变。

    所以,为了完整起见,这个可行...

    <rewrite>
          <rules>
            <rule name="Remove WWW prefix" >
              <match url="(.*)" ignoreCase="true" />
              <conditions trackAllCaptures="false">
                <add input="{HTTP_HOST}" pattern="^www\.mydomain\.com" />
              </conditions>
              <action type="Redirect" url="{MapProtocol:{HTTPS}}://mydomain.com/{R:1}" redirectType="Permanent" />
            </rule>
            <rule name="homepage" stopProcessing="true" >
              <match url="^wwwzone/homepage.aspx"/>
              <action type="Redirect" url="/"/>
            </rule>
          <rule name="homepageReal" stopProcessing="true" >
            <match url="^$"/>
            <action type="Rewrite" url="/somepath/homepage.aspx"/>
          </rule>
          </rules>
          <rewriteMaps>
            <rewriteMap name="MapProtocol">
              <add key="on" value="https"/>
              <add key="off" value="http"/>
            </rewriteMap>
          </rewriteMaps>
        </rewrite>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-21
      • 2019-12-28
      相关资源
      最近更新 更多