Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 668
John Crawford Avatar asked Dec 26 '22 20:12

John Crawford


2 Answers

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)

like image 98
Jon Lin Avatar answered Jan 09 '23 06:01

Jon Lin


I didn't want this to apply to existing files and folder names, so I changed the .htaccess part of Jon Lin's excellent solution to:

RewriteCond $1 [A-Z]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /${lowercase:$1} [R=301,L]
like image 35
Yes Barry Avatar answered Jan 09 '23 05:01

Yes Barry