Bert
Bert

Reputation: 11

htaccess redirect to a specific url on same domain (without looping)

I really hope you can help me out (it is driving me crazy). I've tried dozens of setups and nothing seems to work, Googled myself dizzy and tried numerous different setups, but it all seems to result in a loop or a server error.

This is what needs to happen:

I have a site with multiple domains attached to it. What I need is that when someone visits the website via the "domain.co.uk"-domain, a redirect to the correct language parameters (among others) takes place.

To be very specific: when visiting via "www.domain.co.uk" the visitor must be redirected to "www.domain.co.uk?lang=en&noredir=1&currency=3"

I've made sure that the www is present with this:

RewriteCond %{HTTP_HOST} !^www.domain.co.uk$ [NC]
RewriteRule ^(.*)$ http://www.domain.co.uk/$1 [L,R=301]

The trouble is (I think) the redirect within the same domain without causing a loop.

I've tried stuff like this, but with no result:

RewriteCond %{HTTP_HOST} ^www.domain.co.uk$
RewriteRule ^$ http://www.domain.co.uk/?lang=en&noredir=1&currency=3 [L,R=301]

Hope you can help,

Cheers!

Upvotes: 1

Views: 610

Answers (1)

arco444
arco444

Reputation: 22841

This will cause a loop:

RewriteCond %{HTTP_HOST} ^www.domain.co.uk$
RewriteRule ^$ http://www.domain.co.uk/?lang=en&noredir=1&currency=3 [L,R=301]

Because you're only checking the host header. Every time the redirect fires it will arrive back at the server with a host header of www.domain.co.uk and redirect again. You need to also check the query string and only redirect if it doesn't already match what you sent:

RewriteCond %{HTTP_HOST} ^www.domain.co.uk$
RewrteCond %{QUERY_STRING} !lang=en&noredir=1&currency=3
RewriteRule ^$ http://www.domain.co.uk/?lang=en&noredir=1&currency=3 [L,R=301]

Upvotes: 1

Related Questions