Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert url to lower case using htaccess except query string

Am struggling with a htaccess problem.

I need to convert all the URLs from uppercase to lowercase. But the query string alone should be the same.

For example,

www.tESTUrl.com/sOMePath/?q=SomeStringHere

should be converted as,

www.testurl.com/somepath/?q=SomeStringHere

Please help to fix this. Thanks in advance.

like image 360
Harish Kanakarajan Avatar asked Jun 03 '14 13:06

Harish Kanakarajan


2 Answers

First You have to add this to your httpd.conf:

RewriteMap lc int:tolower

Then paste the below code into your .htaccess

RewriteEngine On
RewriteBase / 
RewriteCond %{REQUEST_URI} ^[^A-Z]*[A-Z].*
RewriteRule ^ ${lc:%{REQUEST_URI}} [L,R=301]

This code redirct the url like from

www.tESTUrl.com/sOMePath/?q=SomeStringHere

to www.testurl.com/somepath/?q=SomeStringHere

like image 174
Prabhu Avatar answered Sep 23 '22 19:09

Prabhu


First you need to add this line in your httpd.conf to define a RewriteMap for handling lower case conversion:

RewriteMap lc int:tolower

Then add this rule in your root .htaccess:

RewriteEngine On

RewriteRule ^(.*?[A-Z]+.*)$ /${lc:$1} [L,NE,R=302]

This will not affect QUERY_STRING.

like image 23
anubhava Avatar answered Sep 26 '22 19:09

anubhava