IIS中的WebConfig URL重写

耶斯伯斯

有一个ASP.NET解决方案,并希望实现以下两点:-

  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,但这是重写而不是重定向。

任何想法表示赞赏。

耶斯伯斯

这反过来使我在正则表达式中的语法不佳。

因此,匹配首页的真实网址应该是

<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>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章