É certamente possível definir variáveis de ambiente usando mod_rewrite
. Usando a seguinte configuração:
RewriteEngine on
RewriteLog /var/log/httpd/rewrite.log
RewriteLogLevel 5
# if the requested resource does not exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# if the users preferred language is supported...
RewriteCond %{HTTP:Accept-Language} ^.*(de|es|fr|it|ja|ru|en).*$ [NC]
# define an environmental variable PREFER_LANG
RewriteRule ^(.*)$ - [env=PREFER_LANG:%1]
# route the uri to a front controller
RewriteRule ^(.*)$ /cgi-bin/serverfault.cgi?q=$1 [PT]
Em que serverfault.cgi
é apenas um script de shell que chama env
. Isso funciona muito bem:
$ curl -s -H 'Accept-language: de' http://localhost/serverfault/somefile |
grep PREFER
PREFER_LANG=de
Como é o seu RewriteLog
? Você deveria ver algo assim:
init rewrite engine with requested uri /serverfault/foo.html
applying pattern '^(.*)$' to uri '/serverfault/foo.html'
RewriteCond: input='/serverfault/foo.html' pattern='!-f' => matched
RewriteCond: input='/serverfault/foo.html' pattern='!-d' => matched
RewriteCond: input='en-US,en;q=0.8' pattern='^.*(de|es|fr|it|ja|ru|en).*$' [NC] => matched
setting env variable 'PREFER_LANG' to 'en'
applying pattern '^(.*)$' to uri '/serverfault/foo.html'
rewrite '/serverfault/foo.html' -> '/cgi-bin/serverfault.cgi?q=/serverfault/foo.html'
split uri=/cgi-bin/serverfault.cgi?q=/serverfault/foo.html -> uri=/cgi-bin/serverfault.cgi, args=q=/serverfault/foo.html
forcing '/cgi-bin/serverfault.cgi' to get passed through to next API URI-to-filename handler