【问题标题】:IIS Rewrite Rule for Lower Case URL's小写 URL 的 IIS 重写规则
【发布时间】:2013-11-28 13:21:23
【问题描述】:

我有一个 IIS 重写规则来将所有 URL 转换为小写

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

但是,它只将部分 url 转换为小写,不转换查询字符串。例如 ID 不转换为 id

http://www.itsmysitesitesite.com/showproduct.aspx?ID=230

如何修改上述规则以包含查询字符串?

【问题讨论】:

  • 虽然这可能不是问题,但你不应该小心吗?您是否 100% 确定这种情况在您的查询字符串中并不重要?
  • 经过一番搜索后,stackoverflow.com/questions/20018553/… 可能会对您有所帮助吗?
  • @Bartdude 重要吗?你能详细说明一下吗?
  • @Damon 是的,我认为这就是我所需要的。但我不想做太多的改变。只是对我已有的配置进行了更改。你能帮忙吗?
  • @user1089173 > 好吧,假设您正在通过查询字符串传递搜索词,区分“MySearchTerm”和“mysearchterm”可能对您很重要,在这种情况下,您不希望要重写的参数。换句话说:虽然在 IIS 上 URL 大小写并不重要,但查询字符串大小写可能很重要。现在我不知道你的情况/网站,也许这只是一句无用的话,但也许这是你忽略的东西。

标签: asp.net iis url-rewriting


【解决方案1】:

根据我的评论(参考this 问题),我想规则看起来像这样:

<rule name="URL Lower" enabled="true" stopProcessing="true">
      <match url="[A-Z]" ignoreCase="false" />                        
      <conditions trackAllCaptures="true">
          <add input="{QUERY_STRING}" pattern="(.*)" />
          <add input="{QUERY_STRING}" pattern="([A-Z]+)" ignoreCase="false" />
      </conditions>
      <action type="Redirect" url="{ToLower:{URL}}{ToLower:{C:1}}" appendQueryString="false" />
 </rule>

但免责声明,我在 Windows XP 上工作,所以我只有 IIS 6.0,所以我无法检查语法是否正确!可能需要一些调整...

【讨论】:

    【解决方案2】:

    我无法制定一条既能捕获{URL}{QUERY_STRING} 又能以我想要的方式显示网址的规则。所以,我把它分成了两条规则。

    小写网址

    <rule name="UrlToLowercase" stopProcessing="true">
      <match url=".*[A-Z].*" ignoreCase="false" />
        <conditions>                          
          <add input="{REQUEST_METHOD}" matchType="Pattern" pattern="POST" ignoreCase="true" negate="true" />
          <add input="{URL}" pattern="^.*\.(axd|css|js|jpg|jpeg|png|gif|txt|xml|svg|pdf)$" negate="true" ignoreCase="true" /> 
        </conditions>
        <action type="Redirect" redirectType="Permanent" url="{ToLower:{URL}}" />
    </rule> 
    

    带有小写查询字符串的 URL

    <rule name="UrlWithQueryStringToLowercase" stopProcessing="true">
      <match url="(.*)" ignoreCase="false" />
        <conditions trackAllCaptures="true">   
          <add input="{QUERY_STRING}" pattern="(.*)" /> 
          <add input="{QUERY_STRING}" pattern=".*[A-Z].*" ignoreCase="false" />
          <add input="{REQUEST_METHOD}" matchType="Pattern" pattern="POST" ignoreCase="true" negate="true" />
        </conditions>
        <action type="Redirect" redirectType="Permanent" url="{ToLower:{URL}}?{ToLower:{C:0}}" appendQueryString="false" />
    </rule>
    

    这应该涵盖所有情况,如果没有,请告诉我。 :)

    我添加了一些您可能想要保留或删除的额外条件。

    &lt;add input="{URL}" pattern="^.*\.(axd|css|js|jpg|jpeg|png|gif|txt|xml|svg|pdf)$" negate="true" ignoreCase="true" /&gt; 忽略以 axd、css、js、jpg、...等结尾的路径。如果没有此规则,您将无法从服务器加载包含大写字母的文件。

    &lt;add input="{REQUEST_METHOD}" matchType="Pattern" pattern="POST" ignoreCase="true" negate="true" /&gt; 将忽略使用 POST 方法的请求。如果没有这个规则,指定大写 url 的 POST 会因为 301 而丢失数据(GET 没有这个问题)。

    【讨论】:

      猜你喜欢
      • 2011-04-21
      • 2014-10-27
      • 2014-05-01
      • 2014-01-05
      • 1970-01-01
      • 2012-06-19
      • 2020-01-05
      相关资源
      最近更新 更多