Search code examples
mod-rewriteurl-rewritingiis-7.5canonical-link

Nested subdomain URL rewrite


I have a sight that is of the following form:

nested_subdomain1.nested_subdomain2.domain.com

It might be something like test.users.domain.com and I would like to be able to rewrite this URL to something like test.users.domain2.com.

So far, my luck has not proven well and I have not been able to successfully implement a working solution from examples found online. I have tried some things like the following:

RewriteCond %{HTTP_HOST} ^domain\.com
RewriteRule (.*) http://domain2.com/$1 [R=301,L]

Or this one...

RewriteCond %{HTTP_HOST} !^fully\.qualified\.domain\.name$ [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://fully.qualified.domain.name/$1 [L,R]

I am not sure what I am doing wrong and feel like I am missing something really obvious.


Solution

  • Try this

    #match anything1.anything2.domain.com
    RewriteCond %{HTTP_HOST} ^([^.]+\.[^.]+)\.domain\.com$ [NC] 
    #redirect to anything1.anything2.domain2.com
    RewriteRule ^ http://%1.domain2.com%{REQUEST_URI} [R=301,L]