正则表达式 – IIS-URL重写,URL规则为小写,但查询字符串除外

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

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)

解决方法

只需要一点修改:打破网址?将文件名部分与查询字符串分开.

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

如果你使用哈希(#),你可能也应该打破它们,因为你不想强迫它们小写.

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

相关文章

正则替换html代码中img标签的src值在开发富文本信息在移动端...
正则表达式
AWK是一种处理文本文件的语言,是一个强大的文件分析工具。它...
正则表达式是特殊的字符序列,利用事先定义好的特定字符以及...
Python界一名小学生,热心分享编程学习。
收集整理每周优质开发者内容,包括、、等方面。每周五定期发...