Search code examples
regexapache.htaccessmod-rewrite

Redirect uppercase URL's to lowercase version specific to a subfolder


I've tried using the mod_spelling but ran into issues, I was hoping to achieve this by just using htaccess. Similar to this ( taken from this post) (however this would lowercase all the URLs I assume)

RewriteEngine On

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

I'll need this to happen in a specific subfolder, anything in this subfolder should 301 to lower case version

For example

www.example.com/subfolder/This-Would/Need2-to_Be-Lower
www.example.com/subfolder/another-Example/Be-Lower/Please_33

After the "subfolder" it shouldn't matter how deep it goes and/or if there are numbers

This is for a joomla site if that helps at all

Any insight is appreciated here


Solution

  • You may use this rule for a subfolder only:

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

    Here the pattern ^subfolder/[^A-Z]*[A-Z] will match a URI that starts with /subfolder/ and has at least one uppercase letter somewhere.