Convert url to lower case using htaccess except query string

独自空忆成欢 提交于 2019-11-27 06:54:32

问题


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.


回答1:


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




回答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.




回答3:


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]



回答4:


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


来源:https://stackoverflow.com/questions/24016728/convert-url-to-lower-case-using-htaccess-except-query-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!