How to rewrite URLs from UPPERCASE to lowercase in .htaccess
If you are on Apache 2.4 then in .htaccess
you can use mod_rewrite with an Apache expression and make use of the tolower()
function. (This doesn't require the use of a RewriteMap
in the server config, as mentioned in comments.) For example:
RewriteEngine On
RewriteCond expr "tolower(%{REQUEST_URI}) =~ /(.*)/"
RewriteRule [A-Z] %1 [L]
The RewriteRule
pattern simply checks there is at least one uppercase letter in the requested URL-path. The RewriteCond
directive then calls the tolower()
function on the URL-path (REQUEST_URI
server variable) which is then effectively captured using the regex. The %1
backreference in the substitution string then holds the result of the tolower()
function call, ie. the lowercased URL-path, which is internally rewritten to.
To "correct" the URL and issue an external redirect, then just add the R
flag to the RewriteRule
directive. For example:
:
RewriteRule [A-Z] %1 [R=301,L]