Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I've got an existing simple rewrite rule like so:

<Directory /path>
    RewriteEngine on
    RewriteBase /

    # if the requested resource does not exist
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    # route the uri to a front controller
    RewriteRule ^(.*)$ index.php/$1 [L]
 </Directory>

This works fine, but I want to do one of either two things. On the basis of detecting the clients accept-language header, I want to either

(i) Set the detected language as an environmental variable that the script can use or

(ii)Rewrite the request so that the url begins with the language code (e.g. www.example.com/en/some/resource)

In terms of implementing (i), I defined this rule:

<Directory /path>
    RewriteEngine on
    RewriteBase /

    # 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 ^(.*)$ index.php/$1 [L]
 </Directory>

I've tried a few variations, but PREFER_LANG is not defined in $_SERVER nor retrievable by getenv.

In terms of implementing (ii)... lets just say its messy. I'll post it if I can't get an answer to one.

Can anyone advise me? Thanks!

share|improve this question

2 Answers 2

According to a comment at PHP manual it's possible. Is PHP integrated as module or CGI?

share|improve this answer

It's certainly possible to set environment variables using mod_rewrite. Using the following configuration:

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]

Where serverfault.cgi is just a shell script that calls env. This works just fine:

$ curl -s -H 'Accept-language: de' http://localhost/serverfault/somefile |
grep PREFER
PREFER_LANG=de

What does your RewriteLog look like? You should see something like this:

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.