Last week, I needed to redirect all requests that contained uppercase letters to their lowercase equivalents for SEO optimization. It turns out we can

How to Redirect Uppercase URLs to Lowercase with Laravel Middleware

submited by
Style Pass
2024-07-11 15:00:07

Last week, I needed to redirect all requests that contained uppercase letters to their lowercase equivalents for SEO optimization.

It turns out we can do this with just a few lines of code in a Laravel middleware! First, we grab the path from the request and check if the it is the same lowercased. If not, we can use the url()->query() method to append the query string back onto the lowercase version of the path and permanently redirect to the lowercased path.

I'm sure there are possible solutions with Nginx or Apache, but this was by far the simplest solution for me and it works across all environments of the app. I don't have to remember to make any changes on new servers.

Leave a Comment