views:

56

answers:

1

Hi guys,

I recently discovered the power of the mod_rewrite module and I need some help with it.

Say I have a website which has two domain names mapping to the same host...

example.com
example.net

And I would like to set a GET var depending on the the HTTP_HOST (i.e. .com OR .net ?) ...

/index.php?lang=en
/index.php?lang=es

However the caveat is this.. I already have 3 RewriteRules...

RewriteRule ^(about|contact)/?$ /index.php?page=$1 [NC]
...
..

And if I were to follow the example over here (bottom of RewriteCond) I would have to copy the 3 RewriteRules for each HTTP_HOST (i.e. .com OR .net) and add the GET var statically like; &lang=en OR &lang=es and the end of each rule...

Is there some way to do this automatically without having to copy the 3 rules all over again which only differ by just one get var at the end?

PS. I know this can be done through the scripting language, but I was wondering if this can be done through mod_rewrite, and how.

thanx!

+2  A: 

Try putting something like this before all your other rules:

RewriteCond %{HTTP_HOST} .com$
RewriteRule ^(.*)$ $1?lang=en [QSA]
RewriteCond %{HTTP_HOST} .net$
RewriteRule ^(.*)$ $1?lang=es [QSA]

Although you might be better off using an environment variable to specify the language, using the SetEnv directive with a different language in each virtual host.

Or maybe even better yet, look into mod_negotiation, which contains some standard capabilities for dealing with languages.

David Zaslavsky
I'll check out that QSA flag.. seems promising.
ninuhadida
While the code submtited was of great help, it's useless on it's own. You'll be left with infinite recursion, since apache will keep on redirecting each page to ?lang=en|es even if this has already been set..So add the following line before: RewriteCond %{QUERY_STRING} !(lang=(en|es))this will not redirect to ?lang=en|es if it has already been set ;)
ninuhadida
There shouldn't be any infinite recursion unless you restart the rule rewriting process at some point later on. From your question I believed you weren't doing that.
David Zaslavsky