views:

17

answers:

1

Hi Guys,

I have a problem where I want to redirect all traffic [301 permanent] from

www.example.com --> www.website2.com
blog.example.com --> blog.example.com

So redirect all domains/subdomains on "Example.com" to website2.com EXCEPT for the blog on example.com ? Little unsure how to set this up using .htaccess on "example.com" ?

Really appreciate any help.

A: 

Somewhat similar to this question:

RewriteCond %{HTTP_HOST} !(^blog\.example\.com$)  
RewriteRule (.*) http://www.website2.com/$1 [R=permanent,QSA,L]

Line by line:

In case the Host: header ("%{HTTP_HOST}") is not ("!") blog.example.com (no other string matches that regex), execute the following rewrite:

for a pattern matching anything (".*", that is, for any URL), redirect to the same path on www.website2.com (e.g. http://blahblah.example.com/somepath will get redirected to http://www.website2.com/somepath ).

Piskvor
thanks a lot - so this takes the reverse approach ? redirect everything BUT blog.example.com ?
Tom
hmm - the only problem with this code is that it doesn't allow for blog.example.com/post-name/ etc ? how to fix this i.e. so it allows for any url on blog.example.com but redirects everything else not on this subdomain ?
Tom
@Tom: Have you tried this, or is this just your speculation? The code will *not* do anything for URLs on blog.example.com, as the second line won't be reached (because the RewriteCond will evaluate to false).
Piskvor
hey piskvor - yeah I tried it a few times. the problem is for example - when i try blog.example.com/wp-admin - it attempts to find http://www.website2.com/wp-admin ? but I basically need anything on the blog.example.com to remain valid - so i.e. blog.example.com/wp-admin or blog.example.com/post-name/ etc and everything else to redirect to website2.com ?
Tom
@Tom: That is strange. Is it really *this* rule causing the redirection? Also, some browsers cache the *permanent* redirection pages, so you could be seeing responses that the server used to send, sometime in the past. Have you tried clearing your browser cache?
Piskvor
hmm maybe it was the cache! :) thx
Tom
@Tom: Most likely, have tested this exact setup here and works as expected. I'm glad that it helped :)
Piskvor