Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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, 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?

like image 350
Anonymous Coward Avatar asked Dec 05 '25 14:12

Anonymous Coward


1 Answers

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]
like image 76
Gumbo Avatar answered Dec 08 '25 17:12

Gumbo