IIS-URL重写,URL规则为小写,但查询字符串除外

时间:2015-05-07 18:25:38

标签: regex asp.net-mvc iis web-config url-rewrite-module

您好我在IIS中使用URL Rewrite模块。 我会创建一个规则来转换小写网址。但是,我遇到了问题。 我的目标是做到以下几点:

http://www.doMain.com/App/ActIon?X=1&y=3&JJ=3... => http://www.domain.com/app/action?X=1&y=3&JJ=3...

我的首次失败尝试是:

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

结果:

http://www.doMain.com/App/ActIon?X=1&y=3&JJ=3... => http://www.domain.com/App/ActIon?X=1&y=3&JJ=3...

它仅适用于域(因为URL变量只有域)

我的第二次失败尝试是:

<rule name="Convert to lower case" stopProcessing="true">  
  <match url=".*[A-Z].*" ignoreCase="false" />  
  <action type="Redirect" url="{ToLower:{R:0}}" redirectType="Permanent" />  
</rule>

结果:

http://www.doMain.com/App/ActIon?X=1&y=3&JJ=3... => http://www.domain.com/app/action?x=1&y=3&jj=3...

这也适用于查询字符串。因此,我没有为我服务。

我的第三次失败尝试是:

<rule name="Convert to lower case" stopProcessing="true">  
  <match url="^(.*[A-Z].*)(\/.*)" ignoreCase="false" />  
  <action type="Redirect" url="{ToLower:{R:1}}{R:2}" redirectType="Permanent" />  
</rule>

结果:

http://www.doMain.com/App/ActIon?X=1&y=3&JJ=3... => http://www.domain.com/app/ActIon?X=1&y=3&JJ=3...

此问题不适用于最后一条路径。

这导致我不需要满足以下规则:

http://www.doMain.com/App => http://www.domain.com/app

问题:

是否有任何规则允许我做我需要的事情?

使用模块是否正确?

因为我有另一种选择是使用ASP.NET路由引擎(例如https://github.com/schourode/canonicalize

2 个答案:

答案 0 :(得分:6)

只需要进行一些修改:打破_-上的URL,将文件名部分与查询字符串分开。

?

如果你使用哈希值(<rule name="Convert to lower case" stopProcessing="true"> <match url="^([^?]*[A-Z][^?]*)(\?.*)?" ignoreCase="false" /> <action type="Redirect" url="{ToLower:{R:1}}{R:2}" redirectType="Permanent" /> </rule> ),你可能也应该打破它们,因为你不想强迫它们小写。

#

答案 1 :(得分:1)

复杂的正则表达式不是必需的

<rule name="Convert to lower case" stopProcessing="true">
  <match url=".*[A-Z].*" ignoreCase="false" />
  <action type="Redirect" 
          url="http://{ToLower:{HTTP_HOST}{PATH_INFO}}" 
          redirectType="Permanent" />
</rule>

Querystring不包含在匹配项中,默认情况下会附加到重定向网址。