views:

110

answers:

2

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: http://stackoverflow.com/questions/703709/rewritemap-activation

+3  A: 
RewriteMap tolower int:tolower
RewriteRule  ^([^/]+)/?$  somedir/${tolower:$1}
Ignacio Vazquez-Abrams
I don't believe this works.
dreeves
Right, I was missing a line. Fixed.
Ignacio Vazquez-Abrams
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.
dreeves
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).
Bee
@Bee: The docs say "server config, virtual host", which means right in the main config stream or within a `<VirtualHost>` directive. http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritemap
Ignacio Vazquez-Abrams
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.
dreeves
That's really odd. I have it working in a `<virtualHost>` section just fine here, so I don't know what's going on.
Ignacio Vazquez-Abrams
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.
dreeves
+2  A: 

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} 
dreeves
Works here. Did you bounce httpd?
Ignacio Vazquez-Abrams
Probably not. What does that mean?
dreeves
Restarting it. Sending it a SIGHUP works on POSIX.
Ignacio Vazquez-Abrams
Thanks for all the help, Ignacio. I updated this answer with explicit instructions about what to put where and about restarting apache.
dreeves