How To Force All URLs To Lowercase In ASP.NET



Last updated: January 28th, 2024

ASP.NET Core

There are two scenarios to consider when making URLs lowercase in ASP.NET Core. One is a URL that's generated by ASP.NET Core and the other is an incoming URL request.

Examples:

  1. Generated URL: ~/Account/Login resolving to: /account/login

  2. Incoming request: https://mysite.com/MYPAGE redirecting to: https://mysite.com/mypage

In the case of a URL redirect, I want to use a 301 status code to make it clear that the real URL is the one that's lowercase.

Both of these URL issues can be addressed in the Program.cs file:

Middleware:

Rule:

ASP.NET

All you have to do in ASP.NET to do this is open up your Web.config (and/ or the transformed version you want to use) and add this part which uses a regex to redirect the URL.

Challenges

While this is all you need to do to have lowercase URLs, from experience you'll want to check for a couple of things. For one, AJAX calls need to use lowercase, if they are using any uppercase characters, the response will be a non-success response and the AJAX calls will fail. Make sure to check all your AJAX calls use lowercase.

Additionally, ASP.NET MVC Razor methods like Html.ActionLink will not automatically make your URLs lowercase. You will have to rely on the redirect to lowercase them, this isn't a big deal because it will 301 redirect to the right URL. It's worth lowercasing them yourself though, if you do not want the 301 to occur.

One last point about this, it doesn't impact the query strings from any of my tests, they will all be whatever case they are on the request.

SEO Implications

One fact about SEO is that each URL is case sensitive. This means even a single character can compromise your content's value to search engines because of duplicate content and authority issues.

Additionally, if people link to you, they aren't always going to take the time to link with the correct casing. I want to capitalize on other people's links by redirecting to the canonical URL (the one with the intended casing). This is done by forcing the URLs to be lowercase through redirecting them to the lowercase version when they are not lowercase.

Another benefit, other than SEO, is that reports like Google Analytics will show the paths consistently for resources, it's easier to see what your top pages are when they are all cased the same.


Comments

No Comments

Post Comment

Prove you are human 11 + 11 =

Tagged: ASP.NET, SEO


Join my email list!



ryan
About Me

With over 15 years in tech, I've excelled as a senior software engineer, specializing in ASP.NET, C#, SQL, Azure, and front-end technologies. I've led diverse projects across various sectors, from startups to global corporations, particularly during my decade in the San Francisco Bay Area.


Sign Up With SoftSys Hosting! (My host)