简体   繁体   English

使用htaccess将网址转换为小写,查询字符串除外

[英]Convert url to lower case using htaccess except query string

Am struggling with a htaccess problem. 我正在为htaccess问题而苦苦挣扎。

I need to convert all the URLs from uppercase to lowercase. 我需要将所有URL从大写转换为小写。 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. 提前致谢。

First You have to add this to your httpd.conf: 首先,您必须将其添加到您的httpd.conf中:

RewriteMap lc int:tolower

Then paste the below code into your .htaccess 然后将以下代码粘贴到您的.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 www.testurl.com/somepath/?q=SomeStringHere

First you need to add this line in your httpd.conf to define a RewriteMap for handling lower case conversion: 首先,您需要在httpd.conf添加此行,以定义RewriteMap来处理小写转换:

RewriteMap lc int:tolower

Then add this rule in your root .htaccess: 然后在您的根.htaccess中添加以下规则:

RewriteEngine On

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

This will not affect QUERY_STRING . 这不会影响QUERY_STRING

I believe tweaking the answer given here can handle the www.tESTUrl.com/ hostname part of the equation: 我相信,调整此处给出的答案可以www.tESTUrl.com/等式的www.tESTUrl.com/主机名部分:

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

And then I believe this should work for the sOMePath/ path: 然后,我认为这应该适用于sOMePath/路径:

RewriteMap lc int:tolower
RewriteRule (.*?[A-Z]+.*)/?(.*)$ ${lc:$1}/?q=$2 [R]
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: 您必须将其添加到您的httpd.conf中:

RewriteMap lc int:tolower

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM