I currently have a pretty straight forward modrewrite file that maps urls to different php pages with possible GET variables:
RewriteRule ^grant$ about.php?p=4 [L]
RewriteRule ^contact-people-([A-za-z0-9-]+)$ about.php?p=5&to=$1 [L]
RewriteRule ^([a-z0-9-]+)-media$ media-gallery-element.php?prettyid=$1 [L]
#etc开发者_运维问答 .... it goes on for perhaps 200 or so entries
I want to have a global language variable that is in the url and I'm wondering how I can smartly adjust my modrewrite file to handle this. I could do it by doubling my entries and just add more rules such that the first one above gets changed to:
RewriteRule ^grant$ about.php?p=4&lan=en [L]
RewriteRule ^([a-z]+)/grant$ about.php?p=4&lan=$1 [L]
The first row is just the same and would default to english (en) and then the second row handles other language codes, es, fr, de, etc. So I could do it this way, but it seems like there is a better way to do this without doubling my entries. Basically, I want to take ALL of my requests, look for a ([a-z]+)/ at the beginning of the request uri, and then tack that onto the request as a GET variable, lan.
How do I do this smartly?
You can do this easily by setting an environmental variable. Something like this should do the trick:
# set default environmental variable for language
RewriteRule .* - [E=language:en]
# if two characters and slash lead, then
RewriteCond %{REQUEST_URI} ^/([a-z]{2})/(.*)$
# overwrite default environmental variable
RewriteRule .* - [E=language:%1]
# other rules
RewriteRule ^grant$ about.php?p=4&lan=%{ENV:language} [L]
You can also access the environmental variable directly in PHP, in which case you could skip including it in the rewrite rules:
echo getenv('language');
echo $_SERVER['language'];
echo $_SERVER['REDIRECT_language'];
精彩评论