简体   繁体   English

IIS中的WebConfig URL重写

[英]WebConfig URL Rewrites in IIS

Have a ASP.NET solution and want to achieve 2 things: - 有一个ASP.NET解决方案,并希望实现以下两点:-

  1. Redirect www.mydomain.com to mydomain.com 将www.mydomain.com重定向到mydomain.com
  2. Have friendly URLs 拥有友好的网址

So ... here is what I have added the system.webserver section of the webconfig... 所以...这就是我添加的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>

This fails. 这失败了。 The logic behind this is: - 其背后的逻辑是:-

  • First rule takes care of redirecting www.mydomain.com to mydomain.com. 第一条规则负责将www.mydomain.com重定向到mydomain.com。 This works. 这可行。 and with the rewriteMap it also handles HTTP/HTTPS correctly. 并使用rewriteMap还可正确处理HTTP / HTTPS。
  • The second rule is supposed to do a browser redirect if they request an unfriendly (real) URL. 如果第二条规则要求浏览器提供不友好的(真实)URL,则应该执行浏览器重定向。
  • The last one is designed to convert the friendly URL back to the real URL, however this is a rewrite not a redirect. 最后一个旨在将友好的URL转换回真实的URL,但这是重写而不是重定向。

Any thoughts much appreciated. 任何想法表示赞赏。

It turns our was poor syntax in my regular expressions. 这反过来使我在正则表达式中的语法不佳。

So, matching the real URL of the home page should have been 因此,匹配首页的真实网址应该是

<match url="^$"/>

not

<match url="^/$"/>

And the subsequent rule to rewrite to the real URL should change in the same way. 并且随后的重写为真实URL的规则应以相同的方式更改。

So, for completeness, this one works... 因此,为了完整起见,这一行有效...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM