'Apache .htaccess convert URL to lowercase if it starts with a certain string

I need to create a redirect rule to lowercase.

All URLs starting with /catalog/ must be lowercase. example:

  • /catalog/Foo => /catalog/foo

  • /catalog/Foo/fOO2 => /catalog/foo/foo2

Can you help me?



Solution 1:[1]

Assuming you are on Apache 2.4 then you can use the tolower() function in an Apache expression in a mod_rewrite RewriteCond directive to perform the uppercase to lowercase conversion on these URLs.

For example:

RewriteEngine On

RewriteCond expr "tolower(%{REQUEST_URI}) =~ /(.*)/"
RewriteRule ^catalog/.*[A-Z] %1 [R=302,L]

The RewriteRule pattern checks for a URL-path that starts catalog/ followed by an uppercase character. The RewriteCond directive then converts the URL-path to lowercase, which is captured in the regex and the corresponding backreference %1 is used in the RewriteRule substitution string as the target for the redirect. %1 contains the lowercase'd URL-path.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 MrWhite