Convert to lowercase in a mod_rewrite rule
Asked Answered
H

3

16

I would like URLs like server.com/foo to be case-insensitive. But server.com/foo actually gets mod_rewrite'd to server.com/somedir/foo

(Assume that all the files in "somedir" are lower case.)

So the question is, how to accomplish a mod_rewrite like the following:

RewriteRule  ^([^/]+)/?$  somedir/convert_to_lowercase($1)

PS: Here's a handy mod_rewrite cheat sheet -- http://dreev.es/modrewrite -- though it fails to answer this particular question.

PPS: Thanks to Bee and Ignacio for all the help with this. Also, here's a related question: RewriteMap activation

Haynie answered 27/5, 2010 at 18:9 Comment(0)
H
21

First, put the following line in the <VirtualHost> section of your .conf file. (For me that lives at /etc/httpd/vhosts.d/00foo.conf.)

RewriteMap lc int:tolower 

You can replace lc with any name you want. Then restart apache, which you can do with sudo service httpd restart.

Finally, add this in your .htaccess file:

RewriteRule ^/(.*)$ /${lc:$1} 
Haynie answered 27/5, 2010 at 19:7 Comment(5)
Works here. Did you bounce httpd?Smew
Probably not. What does that mean?Haynie
Thanks for all the help, Ignacio. I updated this answer with explicit instructions about what to put where and about restarting apache.Haynie
Be advised: This may not work as advertised. The first part is correct. Put the RewriteMap statement in the .conf file. However in the .htaccess file, you may need one more statement; "RewriteEngine On" This is what finally worked for me. RewriteEngine On RewriteCond %{REQUEST_URI} [A-Z] RewriteRule . ${lc:%{REQUEST_URI}} [R=301,L]Stomatitis
"add this in your .htaccess file:" - although the directive as posted will only work in a server or virtualhost context.Chamaeleon
S
14
RewriteMap tolower int:tolower
RewriteRule  ^([^/]+)/?$  somedir/${tolower:$1}
Smew answered 27/5, 2010 at 18:20 Comment(6)
Ah, thanks, it now matches what I found elsewhere on the web in the meantime, but it's still not working for me. I didn't understand your other comment about bouncing httpd.Haynie
I tried adding the above to my .htaccess and restarting httpd, and it is bringing the whole domain down. The error log says: "Rewritemap not allowed here". Where specifically am I supposed to put the Rewritemap statement? The documentation I'm finding shows it as two consecutive lines, like above. (I just stuck it in .htaccess right above the specific RewriteRule).Kessinger
@Bee: The docs say "server config, virtual host", which means right in the main config stream or within a <VirtualHost> directive. httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritemapSmew
Sorry we're so dense about this stuff. I see from the link you provided (thanks!) that the RewriteMap line is not allowed in .htaccess. I tried putting in the <VirtualHost> section of /etc/httpd/vhosts.d/00server.conf but when I restart Apache it complains that RewriteMap is not allowed here.Haynie
That's really odd. I have it working in a <virtualHost> section just fine here, so I don't know what's going on.Smew
Ah, sorry, it worked there after all! I'll update my answer with more explicit instructions about this. If you want to do the same then I can just delete my answer.Haynie
N
3

I would make it a 301 redirect, NOT a URL rewrite, for SEO purposes:

RewriteMap tolower int:tolower
RewriteRule  ^([^/]+)/?$  somedir/${tolower:$1} [R=301,L]
Nevins answered 24/7, 2013 at 15:0 Comment(2)
A 301 without a cache expiry header is very bad practice because it gets cached for ever in browsers. You'll be living with its effects for years. Change R=301,L to R=301,L,E=limitcache:1 and add a Header always set Cache-Control "max-age=3600" env=limitcache directive. That'll limit caching to 1h while still giving you the SEO advantages of a 301 redirect.Suffering
A 301 redirect is, by definition, permanent, thus it can be cached indefinitely by default. If you expect the redirect not to be permanent, then use 302.Apophysis

© 2022 - 2024 — McMap. All rights reserved.