2

My idea is:

  • requests to service.example.com should be redirected to service.example.com,
  • all the other requests to example.com should be redirected to base.example.com

My host is configured in such a way, that there is example.com directory, and all subdomains live in their subdirectories (example.com/service, example.com/base, etc.).

How can I achieve the needed redirections?

I have tried with RedirectMatch, RewriteCond, RewriteRule, but with no results. My try was:

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

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

It worked only for the base.example.com. For service.example.com, there was loop of rewrites/redirections.

I could only find examples of simple rewrites, one domain -> other domain, or many domains -> one domain, yet my case is different: its many domains -> many other domains.

EDIT: OK, I got it with:

RewriteCond %{HTTP_HOST} ^service\.example\.com$ [NC]
RewriteRule ^(.*)$ - [L]

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

The key was to use a rule to do nothing: - [L].

0
RewriteCond %{HTTP_HOST} ^service\.example\.com$ [NC]
RewriteRule ^(.*)$ - [L]

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

Or, simply...

RewriteCond %{HTTP_HOST} !^(base|service)\.example\.com$
RewriteRule (.*) http://base.example.com/$1 [R=301,L]

HOWEVER... If you are placing this .htaccess file in the parent directory (which I assume you must be) then this probably won't work as intended, since the "hidden" subdirectory (that the subdomain points to) will also be captured by the RewriteRule pattern and included in the redirect. (Or is that the intention?!)

For example, given a subdomain foo that points to the corresponding subdirectory example.com/foo and you request http:://foo.example.com/bar, then the above directive will redirect the request to http://base.example.com/foo/bar, not http://base.example.com/bar (which I assume would be the intention). This is because the RewriteRule pattern captures the file-path (that the URL maps to) in this context, not the URL-path.

To resolve this, you would need to use the REQUEST_URI server variable instead, which holds the requested root-relative URL-path.

For example:

RewriteCond %{HTTP_HOST} !^(base|service)\.example\.com$
RewriteRule ^ http://base.example.com%{REQUEST_URI} [R=302,L]

Test first with a 302 (temporary) redirect to avoid potential caching issues and only change it to 301 (permanent) - if that is the intention - once you have confirmed that it all works OK.

You will need to clear your browser cache before testing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.