Forum Moderators: phranque

Message Too Old, No Replies

RewriteCond on subdomains to pass on a variable

         

klelugi

5:48 pm on Mar 22, 2010 (gmt 0)

10+ Year Member



Hi,

I can't figure out what the right rewritecond or rewriterule syntax should be for my problem. What I actually would like to do is :

I have :

es.domain.com
de.domain.com
it.domain.com

and I would like to ask for the url :

es.domain.com/?lang=es or
de.domain.com/?lang=de + whatever the query will be after and so on for any added language

All this with no redirect.

What would be the right syntax for that ?

I have that but it won't work :


RewriteCond %{HTTP_HOST} ^es\.domain\.com
RewriteRule .* /?lang=es [L]


Many thanks for your help

phranque

6:38 pm on Mar 22, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



you want a regular expression in the RewriteCond and probably the QSA [httpd.apache.org] flag in the RewriteRule:

RewriteCond %{HTTP_HOST} ^([a-z][a-z])\.domain\.com
RewriteRule .* /?lang=%1 [L, QSA]

or maybe this:
RewriteCond %{HTTP_HOST} ^([a-z][a-z])\.domain\.com
RewriteRule (.*) /$1?lang=%1 [L, QSA]

klelugi

6:47 pm on Mar 22, 2010 (gmt 0)

10+ Year Member



Thanks for your answer,

I thought that this


RewriteCond %{HTTP_HOST} ^([a-z][a-z])\.domain\.com
RewriteRule (.*) /$1?lang=%1 [L, QSA]


or this


RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com
RewriteRule (.*) /$1?lang=%1 [L, QSA]


would actually do the job, but it curiously returns a 500... and I can't figure out why ...?

phranque

7:33 pm on Mar 22, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



look in your server error log for clues.

klelugi

7:55 pm on Mar 22, 2010 (gmt 0)

10+ Year Member



I will, many thanks for your help

jdMorgan

10:48 pm on Mar 22, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



One reason is likely because this creates an infinite loop, since *any* URL is rewritten to itself, plus the current query string, plus a new lang= query parameter. Your code, if implemented in .htaccess, must explicitly prevent this from happening.

There is also a spurious space character in the flags -- Mod_rewrite syntax is *not* free-form; It must be written exactly as specified in the documentation. I'd suggest:

RewriteCond %{QUERY)STRING} !^([^&]*&)*lang=[^&]+
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com
RewriteCond %1 !=www
RewriteRule ^(.*)$ /$1?lang=%1 [QSA,L]

Jim

phranque

2:13 am on Mar 23, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



i should know better than to attempt mod_rewrite code without proper rest.
or testing...
=8)

klelugi

11:59 am on Mar 23, 2010 (gmt 0)

10+ Year Member



Many thanks for your help Jim

Actually this code returns a 500 as well, any clue why this won't work ?

I have used only these few lines to make sure no other rewriterules won't interfere :


RewriteEngine On

RewriteCond %{QUERY)STRING} !^([^&]*&)*lang=[^&]+
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com
RewriteCond %1 !=www
RewriteRule ^(.*)$ /$1?lang=%1 [QSA,L]