Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
633 views
in Technique[技术] by (71.8m points)

regex - Convert to lowercase in a mod_rewrite rule

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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} 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...