Convert url to lowercase using htaccess except query string

Fighting the htaccess problem.

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

For instance,

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

should be converted as

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

Please help fix this. Thanks in advance.

+6
source share
4 answers

First you must add this to your httpd.conf:

 RewriteMap lc int:tolower 

Then paste the code below into .htaccess

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

This code resets the url for example from

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

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

+7
source

First you need to add this line to your httpd.conf to define a RewriteMap to handle lowercase conversion:

 RewriteMap lc int:tolower 

Then add this rule to your root.htaccess:

 RewriteEngine On RewriteRule ^(.*?[AZ]+.*)$ /${lc:$1} [L,NE,R=302] 

This will not affect QUERY_STRING .

+1
source

I believe that setting up the answer given here can handle part of the host name www.tESTUrl.com/ :

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

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

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

The second line is not needed, but useful if you want to further query the line. I just put it here to be complete.

You should add this to your httpd.conf:

 RewriteMap lc int:tolower 
0
source

Source: https://habr.com/ru/post/970241/


All Articles