locked
How to redirect URLs with uppercase to corresponding lower ones? RRS feed

  • Question

  • User-1600963911 posted

    I have added the following to RegisterRoutes(RouteCollection routes) of Global.asax.cs:

    routes.LowercaseUrls = true;

    It works great. For SEO purpose, I would like to redirect old URLs with uppercase letters to their corresponding lowercase ones.  For example:

    /Home/Foo1/Foo2   to    /home/foo1/foo2

    /Apps/Foo3             to    /apps/foo3

    Is there a way to do this globally instead of dealing with each controller and action one by one?

    Monday, February 11, 2019 2:54 PM

Answers

  • User1520731567 posted

    Hi zipswich,

    I would like to redirect old URLs with uppercase letters to their corresponding lowercase ones. 

    You could add the below code in Application_BeginRequest in Global.asax:

    protected void Application_BeginRequest(object sender, EventArgs e)
    {
      //You don't want to redirect on posts, or images/css/js
      bool isGet = HttpContext.Current.Request.RequestType.ToLowerInvariant().Contains("get");
      if (isGet && HttpContext.Current.Request.Url.AbsolutePath.Contains(".") == false)    
      {
         string lowercaseURL = (Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority + HttpContext.Current.Request.Url.AbsolutePath);
         if (Regex.IsMatch(lowercaseURL, @"[A-Z]"))
         {
          //You don't want to change casing on query strings
          lowercaseURL = lowercaseURL.ToLower() + HttpContext.Current.Request.Url.Query;
    
          Response.Clear();
          Response.Status = "301 Moved Permanently";
          Response.AddHeader("Location", lowercaseURL); 
          Response.End();
        }
     }
    }

    Best Regards.

    Yuki Tao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 12, 2019 9:13 AM
  • User-2054057000 posted

    To redirect upper case url to lower case url you enter the below code in your web.config file:

    <rewrite>
      <rules>
        <rule name="LowerCaseRule">
          <match url="[A-Z]" ignoreCase="false"/>
          <conditions>
            <add input="{REQUEST_URI}" pattern="^/*" negate="true"/>
          </conditions>
          <action type="Redirect" url="{ToLower:{URL}}"/>
        </rule>
      </rules>
    <rewrite>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 12, 2019 11:16 AM

All replies

  • User1520731567 posted

    Hi zipswich,

    I would like to redirect old URLs with uppercase letters to their corresponding lowercase ones. 

    You could add the below code in Application_BeginRequest in Global.asax:

    protected void Application_BeginRequest(object sender, EventArgs e)
    {
      //You don't want to redirect on posts, or images/css/js
      bool isGet = HttpContext.Current.Request.RequestType.ToLowerInvariant().Contains("get");
      if (isGet && HttpContext.Current.Request.Url.AbsolutePath.Contains(".") == false)    
      {
         string lowercaseURL = (Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority + HttpContext.Current.Request.Url.AbsolutePath);
         if (Regex.IsMatch(lowercaseURL, @"[A-Z]"))
         {
          //You don't want to change casing on query strings
          lowercaseURL = lowercaseURL.ToLower() + HttpContext.Current.Request.Url.Query;
    
          Response.Clear();
          Response.Status = "301 Moved Permanently";
          Response.AddHeader("Location", lowercaseURL); 
          Response.End();
        }
     }
    }

    Best Regards.

    Yuki Tao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 12, 2019 9:13 AM
  • User-2054057000 posted

    To redirect upper case url to lower case url you enter the below code in your web.config file:

    <rewrite>
      <rules>
        <rule name="LowerCaseRule">
          <match url="[A-Z]" ignoreCase="false"/>
          <conditions>
            <add input="{REQUEST_URI}" pattern="^/*" negate="true"/>
          </conditions>
          <action type="Redirect" url="{ToLower:{URL}}"/>
        </rule>
      </rules>
    <rewrite>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 12, 2019 11:16 AM
  • User-1600963911 posted

    Thank you, Yuki.  This works.

    Tuesday, February 12, 2019 12:34 PM
  • User-1600963911 posted

    Thank you, Yogygoyi.  It looks good though I have not tried this.  Does the rule generate 301 permanent redirect?

    Tuesday, February 12, 2019 12:38 PM
  • User-2054057000 posted

    zipswich

    Does the rule generate 301 permanent redirect?

    Yes 301 redirects.

    Tuesday, February 12, 2019 12:42 PM
  • User-1600963911 posted

    Yes 301 redirects.

    Great!  Thank you.

    Tuesday, February 12, 2019 12:47 PM