1

What is the best way to force lower case URLs on rewrite?

  • Global.asax?
  • web.config?

All has failed...what am I doing wrong? See the following:

Global.asax

I don't know which method to use?

void Application_BeginRequest(object sender, EventArgs e)
    {
        HttpContext context = HttpContext.Current;
        string path = Request.Path.ToLower();

        context.RewritePath(path);
    }

web.config

I have tried using the web.config method but can't manage:

<rewrite url="(.*)$" to="$1" permanent="true" />

How can I change the above code so that it automatically force the url to lower case?

4

3 回答 3

2

您是否尝试过 IIS 的 URL 重写模块?

http://www.iis.net/download/urlrewrite

小写有一个特殊的规则: 在此处输入图像描述

于 2012-05-02T10:38:23.227 回答
1

我们没有办法强迫你your user写小写的网址。生成的所有 urlyou都可以是小写。如果您的 url 中出现大写字母,您可以将用户重定向到小写字母版本。

void Application_BeginRequest(object sender, EventArgs e)
{
    string path = Request.Path;
    if (ContainsUpperChars(path))
    {
        HttpContext.Current.Response.Redirect(Request.Path.ToLower());
    }
}

bool ContainsUpperChars(string str) { some code to test for uppercase chars }
于 2012-05-02T09:52:53.003 回答
1

在 web.config 中使用 url rewrite 标签。如果 vs2012 无法识别该标签,请运行此页面上的脚本:http://blog.vanmeeuwen-online.nl/2013/04/visual-studio-2012-xml-intellisense-for.html 是另外两个链接我过去常常想办法。 ScottGu的博客和 Meligy 的博客

<system.webServer>                                               
<rewrite>
            <rules>
                <rule name="LowerCaseRule" stopProcessing="true">
                    <match url="[A-Z]" ignoreCase="false" />
                    <action type="Redirect" url="{ToLower:{URL}}" />
                </rule>
            </rules>
        </rewrite>
</system.webServer>
于 2016-02-10T15:30:43.487 回答