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

时间:2022-11-24 08:21:55

Hi I am using the URL Rewrite module in IIS. I would create a rule to convert lowercase urls. But, I'm having problems. My goal is to do the following:

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

My first failed attempt was:

我的第一次失败尝试是:

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

Result:

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

it only applies to the domain (Because the URL variable has only the domain)

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

My second failed attempt was:

我的第二次失败尝试是:

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

Result:

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

This also applies to query string. Therefore I did not serve me.

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

My third failed attempt was:

我的第三次失败尝试是:

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

Result:

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

The problem this does not apply to the last path.

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

This causes the following rule that I need is not met:

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

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

Questions:

Is there any rule that allows me to do what I need?

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

Is it correct to do with the module?

这个模块是否正确?

Because I have another alternative is to use ASP.NET routing engine (For example https://github.com/schourode/canonicalize)

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

2 个解决方案

#1


There is just a little modification needed: Break the URL on the ? to separate the filename part from the query string.

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

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

If you use hashes (#), you probably should break on them too, because you don't want to force them to lowercase neither.

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

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

#2


The complex regex is not necessary

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

<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 is not included in the match, and is appended to the redirect URL by default unchanged.

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

#1


There is just a little modification needed: Break the URL on the ? to separate the filename part from the query string.

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

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

If you use hashes (#), you probably should break on them too, because you don't want to force them to lowercase neither.

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

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

#2


The complex regex is not necessary

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

<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 is not included in the match, and is appended to the redirect URL by default unchanged.

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