嗨,我在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>