7

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.

4 Answers 4

9

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

1
  • REQUEST_URI do not contain the domain name, only the URL path. Aug 24, 2015 at 14:00
2

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.

12
  • Thanks @anubhava.. Please let me know where should I exactly place the RewriteMap ?? Sorry I am new to it. Jun 3, 2014 at 14:09
  • It can be placed at the bottom of httd.conf. You will need to restart Apache after that change.
    – anubhava
    Jun 3, 2014 at 14:10
  • Let me check it. Thanks Jun 3, 2014 at 14:21
  • Sorry anubhava its not working.. I am using xampp.. Windows OS.. Anything else should I do..? Jun 3, 2014 at 14:22
  • Did you restart Apache after adding this line? This is properly tested rule and working on my side.
    – anubhava
    Jun 3, 2014 at 14:26
0

I believe tweaking the answer given here can handle the www.tESTUrl.com/ hostname part of the equation:

RewriteCond %{HTTP_HOST} !^(.+\.)?(.+)?$
RewriteRule ^(.*)$ ${lowercase:%{HTTP_HOST}}/$1 [R=301,L]

And then I believe this should work for the sOMePath/ path:

RewriteMap lc int:tolower
RewriteRule (.*?[A-Z]+.*)/?(.*)$ ${lc:$1}/?q=$2 [R]
0
RewriteCond %{REQUEST_URI} ^[^A-Z]*[A-Z].* [OR]
RewriteCond %{QUERY_STRING} ^[^A-Z]*[A-Z].*
RewriteRule ^ ${lc:%{REQUEST_URI}}?${QUERY_STRING} [L,R=301]

The second line is not necessary but helpful if you want to lowercase the query string in the future. I just put this here to be complete.

You have to add this to your httpd.conf:

RewriteMap lc int:tolower
2
  • Can you please tel me whether I need to enclose the RewriteMap code in any <ifmodule> tag like that..? Jun 3, 2014 at 14:25
  • You can put it in the VirtualHost entry: "<VirtualHost *> RewriteMap lc int:tolower" ....
    – unicorn80
    Jun 3, 2014 at 14:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.