开发者

Using mod_rewrite, how do I force the path and query string to be all lower case?

开发者 https://www.devze.com 2022-12-09 14:59 出处:网络
This seems like it should be an easy thing to do, but for the life of me I can\'t figure this out. I want to force my entire URL to be in lower case, so that, for example:

This seems like it should be an easy thing to do, but for the life of me I can't figure this out.

I want to force my entire URL to be in lower case, so that, for example:

http://www.EXAMPLE.com/foo?q=bar
http://www.example.com/FOO?q=bar
http://www.example.com/foo?Q=BAR
http://www.EXAMPLE.com/FOO?Q=BAR

all (301) redirect to:

http://www.example.com/foo?q=bar

Adding:

RewriteMap  lc int:tolower

to httpd.conf, and:

RewriteEngine on
RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule (.*) ${lc:$1} [L,R=301]

to .htaccess,开发者_StackOverflow社区 I can make the base part of the URL redirect the way I want (the first two cases above), but I can't figure out how to make this work with for the query string. Can anyone point me in the direction of how to do this?


Try this rule:

RewriteCond %{REQUEST_URI} ^[^A-Z]*[A-Z].* [OR]
RewriteCond %{QUERY_STRING} ^[^A-Z]*[A-Z].*
RewriteRule ^ ${lc:%{REQUEST_URI}}?${lc:%{QUERY_STRING}} [L,R=301]


Give this a shot, I didn't test it:

RewriteMap lowercase int:tolower
RewriteCond $1 [A-Z]
RewriteRule ^/(.*)$ /${lowercase:$1} [R=301,L]
0

精彩评论

暂无评论...
验证码 换一张
取 消