【问题标题】:IIS-URL Rewrite, URL rule to lowercase except querystringIIS-URL 重写,URL 规则为小写,除了查询字符串
【发布时间】:2015-05-07 18:25:37
【问题描述】:

您好,我在 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

【问题讨论】:

  • 我认为这与 URL 重写有关。只需使用 routes.LowercaseUrls 配置选项并称之为一天。这至少可以确保您生成的所有 URL 都是小写的。如果你只是将它用于小写,即使是 Canonicalize 项目也似乎有点过头了。
  • 感谢您的评论,但仅此一项就是注册所有路由(使用小写字母)。当用户对路径上包含大写字母的 URL 进行 GET 操作时,我需要自动重定向。

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


【解决方案1】:

只需要稍作修改:断开 ? 上的 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>

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

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

【讨论】:

  • 这很好用,但是我的网站以子目录 www.mysite.com/English/home.aspx www.mysite.com/French/home.aspx 开始应用上述规则时,英文和法语没有转换为小写
【解决方案2】:

不需要复杂的正则表达式

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

查询字符串不包含在匹配中,默认情况下附加到重定向 URL 后不变。

【讨论】:

    猜你喜欢
    • 2014-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-24
    • 2016-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多