【问题标题】:Url rewrite to lower case not working网址重写为小写不起作用
【发布时间】:2013-09-16 16:16:05
【问题描述】:

我的 web.config 中有以下重写规则。规范规则有效,但小写规则无效。

我正在尝试像这样对其进行测试:www.mysite.com/UPPERCASE。我本来希望将 url 转换为 www.mysite.com/uppercase,但它仍然是大写的。我做错了什么?

<rewrite xdt:Transform="Insert">
  <rules>
    <rule name="LowerCaseRule" patternSyntax="ExactMatch">
      <match url="[A-Z]" ignoreCase="false"/>
      <action type="Redirect" url="{ToLower:{URL}}"/>
    </rule>
    <rule name="CanonicalHostName">
      <match url="(.*)" />
      <conditions logicalGrouping="MatchAll">
        <add input="{HTTP_HOST}" pattern="^www.mysite.com$" negate="true" />
      </conditions>
      <action type="Redirect" url="{MapSSL:{HTTPS}}www.mysite.com/{R:1}" redirectType="Permanent" />
    </rule>
  </rules>
  <rewriteMaps>
    <rewriteMap name="MapSSL" defaultValue="OFF">
      <add key="ON" value="https://" />
      <add key="OFF" value="http://" />
    </rewriteMap>
  </rewriteMaps>
</rewrite>

【问题讨论】:

    标签: asp.net url-rewriting web-config


    【解决方案1】:

    这对我来说更好。我注意到{URL} 无法正确解析当您有类似cassete.axd/scripts/myscript.js?xxx 的路径时,它会重定向到cassette.axd?xxx

        <rule name="LowerCaseRule - HTTPS">
          <match url="[A-Z]" ignoreCase="false"/>
          <conditions>
            <add input="{HTTPS}" pattern="on" ignoreCase="true"/>
          </conditions>
          <action type="Redirect" url="https://{ToLower:{HTTP_HOST}}{ToLower:{PATH_INFO}}" appendQueryString="true"/>
        </rule>
    
        <rule name="LowerCaseRule - HTTP">
          <match url="[A-Z]" ignoreCase="false"/>
          <conditions>
            <add input="{HTTPS}" pattern="off" ignoreCase="true"/>
          </conditions>
          <action type="Redirect" url="http://{ToLower:{HTTP_HOST}}{ToLower:{PATH_INFO}}" appendQueryString="true"/>
        </rule>
    

    希望这对某人有所帮助。

    【讨论】:

      【解决方案2】:

      试试这个

      <rule name="LowerCaseRule" stopProcessing="true">
              <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
              <add input="{URL}" pattern=".*[A-Z].*" ignoreCase="false" />
              </conditions>
              <action type="Redirect" url="{ToLower:{URL}}" />
          </rule>
      

      【讨论】:

        【解决方案3】:

        您应该从规则 LowerCaseRule 中删除 patternSyntax="ExactMatch",因为在您的情况下,您希望使用正则表达式系统(默认情况下或通过设置 patternSyntax="ECMAScript")。

        所以你的规则应该是:

        <rule name="LowerCaseRule">
          <match url="[A-Z]" ignoreCase="false"/>
          <action type="Redirect" url="{ToLower:{URL}}"/>
        </rule>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-07-10
          • 2013-09-22
          • 2015-09-10
          • 1970-01-01
          • 2016-01-09
          • 1970-01-01
          • 2014-11-15
          相关资源
          最近更新 更多