Using mod_rewrite, how do I force the path and query string to be lowercase?

It seems like it should be easy, but for me life I cannot understand.

I want my entire url to be in lowercase, so 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

      

in httpd.conf and:

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

      

to.htaccess, I can make the base url portion redirected the way I want (the first two cases above), but I cannot figure out how to make this work for the query string. Can anyone point me to how to do this?

+2


source to share


2 answers


Try the following 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]

      

0


source


Give this snapshot, I haven't tested it:



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

      

0


source







All Articles