How do I always keep my Urls in lower case?

Asked

Viewed 578 times

3

I want all Urls on my site to be lowercase to help with SEO and to make link sharing consistent.

How can I do that?

2 answers

7


You can use the IIS URL Rewrite. To use on a server just download and install. Shared hosts usually include this module.

With this module installed just configure rules.

To convert everything to minuscule it is possible to use this rule.

<system.webServer>
<rewrite>
    <rule name="LowerCase" stopProcessing="true">
      <match url=".*[A-Z].*" ignoreCase="false" />
      <conditions>
        <add input="{REQUEST_METHOD}" matchType="Pattern" pattern="GET" ignoreCase="false" />
      </conditions>
      <action type="Redirect" url="{ToLower:{R:0}}" redirectType="Permanent" />
    </rule>
</rewrite>

It will detect if there are uppercase letters in the URL, in which case it will redirect to the minuscule equivalent. Note that this rule only redirects the method GET, thus preventing the sending of a form or a request incorrect external.

And for your code to generate Urls in minusculo you must arrow the following property to true (only available on . NET 4.5):

public static void RegisterRoutes(RouteCollection routes)
{
    routes.LowercaseUrls = true;
}

If you do not have the IIS URL Rewrite you can do by code, on Global.asax.cs

protected void Application_BeginRequest(object sender, EventArgs e)
{
    if (Regex.IsMatch(Request.Url.OriginalString, @"[A-Z]"
        && HttpContext.Current.Request.HttpMethod == "GET")
    {
        Response.Clear();
        Response.Status = "301 Moved Permanently";
        Response.AddHeader("Location", Request.Url.OriginalString.ToLower());
        Response.End();
    }
}
  • Is it really necessary to write that? I usually just define RouteCollection.LowercaseUrls as true and get the desired result.

0

I use a slightly different method to do, and avoid modifying Querystring to avoid problems, sometimes the parameter may be necessary case sensitive or if it is a Bundle or urls that can be sensitive to case sensitive and make no difference in what the user views

 protected void Application_BeginRequest(object sender, EventArgs e)
    {
        var isGet = HttpContext.Current.Request.RequestType.ToLowerInvariant().Contains("get");
        if (!isGet || HttpContext.Current.Request.Url.AbsolutePath.Contains(".")) return; // não desejo redirecionar em caso de POST, ou imagens/css/js/etc
        if (HttpContext.Current.Request.Url.AbsolutePath.Contains("/Content/") || HttpContext.Current.Request.Url.AbsolutePath.Contains("/Scripts/") || HttpContext.Current.Request.Url.AbsolutePath.Contains("/bundles/") || HttpContext.Current.Request.Url.AbsolutePath.Contains("/lightview/")) return; // proteção adicional ao arquivos das pastas content (css) e scripts (js)
        var lowercaseUrl = (Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority + HttpContext.Current.Request.Url.AbsolutePath);
        if (!Regex.IsMatch(lowercaseUrl, @"[A-Z]")) return;
        lowercaseUrl = lowercaseUrl.ToLower() + HttpContext.Current.Request.Url.Query; // Não devo alterar o case da querystring
        Response.Clear();
        Response.Status = "301 Moved Permanently";
        Response.StatusCode = (int)HttpStatusCode.MovedPermanently;
        Response.AddHeader("Location", lowercaseUrl);
        Response.End();
    }      

}

This avoids me some problems and leaves only the necessary in the pattern. I also add the 301 header to avoid problems with Google

Browser other questions tagged

You are not signed in. Login or sign up in order to post.