微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

正则表达式 – 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>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐