John Crawford John Crawford - 21 days ago 5
Apache Configuration Question

Force lower case of URLs - Override previous rules

I have inherited a rather scary looking .htaccess file that is filled with previous rules. What I am trying to do is simply make every single URL lowercase for SEO reasons. Currently Google Webmasters is complaining about duplicate pages. eg: www.example.com/AbC1.php has the same content as www.example.com/abc1.php. To solve this I placed the following lines into my vhosts.conf

#Make URL's lower case
RewriteEngine On
RewriteMap lowercase int:tolower
RewriteCond \$1 [A-Z]
RewriteRule ^/(.*)$ /\${lowercase:\$1} [R=301,L]


But due to one of the many rules I have in my .htaccess file this rule isn't working. Can I add that above rule and ensure that it overrides any other rules?

Answer

You can add it above any other rules in your htaccess file but the rewrite map definition must be in your vhost config, so in vhost:

RewriteEngine On
RewriteMap lowercase int:tolower

And in the very top of your htaccess file:

RewriteCond $1 [A-Z]
RewriteRule ^(.*)$ /${lowercase:$1} [R=301,L]

(note that you don't need to leading slash)