Web Servers on Unix and Linux - mod_rewrite problem

This is Interesting: Free IT Magazines  
Home > Archive > Web Servers on Unix and Linux > February 2006 > mod_rewrite problem





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author mod_rewrite problem
Manfred Kooistra

2006-02-02, 5:51 pm

I am trying to redirect visitors of my home directory to different
subdirectories, depending on the accept language header sent by the
browser. For this I wrote a .htaccess file with the following content:

RewriteEngine On

RewriteCond %{HTTP_ACCEPT_LANGUAGE} ^de [NC]
RewriteRule ^/ http://www.domain.com/de/ [L,R=301]

RewriteCond %{HTTP_ACCEPT_LANGUAGE} ^en [NC]
RewriteRule ^/ http://www.domain.com/en/ [L,R=301]

RewriteRule ^/ http://www.domain.com/no/ [L,R=301]

But when I go to www.domain.com (with my browser set to "de" or "en" -
I checked that this is indeed transferred), I always end up in the
.../no directory. Does RewriteCond not understand the
HTTP_ACCEPT_LANGUAGE variable, or did I make some mistake?

All this is happening on Apache 1.3.33

(And yes, I know that I can direct visitors to different files using
AddLanguage etc., but I want to direct them to subdirectories.)

Andreas Prilop

2006-02-03, 5:53 pm

On 2 Feb 2006, Manfred Kooistra wrote:

> RewriteCond %{HTTP_ACCEPT_LANGUAGE} ^en [NC]
> RewriteRule ^/ http://www.domain.com/en/ [L,R=301]
> RewriteRule ^/ http://www.domain.com/no/ [L,R=301]


"no" is a bad choice, because it would suggest "Norwegian".

> Does RewriteCond not understand the
> HTTP_ACCEPT_LANGUAGE variable,


So it is.
http://httpd.apache.org/docs/1.3/mo...tml#RewriteCond

> or did I make some mistake?


Yes, you tried to solve a problem with mod_rewrite where you
should use other tools.

> (And yes, I know that I can direct visitors to different files using
> AddLanguage etc.,


Fine, then. Problem solved.

Manfred Kooistra

2006-02-03, 5:53 pm


Andreas Prilop schrieb:

> "no" is a bad choice, because it would suggest "Norwegian".


It is in fact supposed to mean "Norwegian" - that is the default
language in my example.

[vbcol=seagreen]
>
> Yes, you tried to solve a problem with mod_rewrite where you
> should use other tools.


Well, there are quite a few examples on the web, where people utilize
HTTP_ACCEPT_LANGUAGE in exact the same way as I tried to do. Obviously
they were all mistaken. Strange.

Anyway, how can I get a redirect into a subdirectory based on
HTTP_ACCEPT_LANGUAGE? AddLanguage does not do this (it only adds an
extension), and I cannot limit it to the current directory (because it
works on all subdirectories as well). So how to do it?

Manfred Kooistra

2006-02-05, 3:22 pm

Andreas, this is my solution to the problem (example for two
languages):

RewriteEngine On

RewriteBase /

RewriteCond %{HTTP:Accept-Language} ^.*de.*$ [NC]
RewriteRule ^(index\.php)?$ http://www.domain.com/de/ [L,R=301]

RewriteCond %{HTTP:Accept-Language} ^.*en.*$ [NC]
RewriteRule ^(index\.php)?$ http://www.domain.com/en/ [L,R=301]

RewriteRule ^(index\.php)?$ http://www.domain.com/de/ [L,R=301]

Yes, the regular expression will be refined not to serve "de" to
someone prefering English but knowing German, nor to someone not
wanting German and stating "de; q=0".

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com