There is an old site on a subdomain, for example 2015.site.ru

changed the main site.ru domain to 2015.site.moscow

now it is necessary that all calls to 2015.site.ru are redirected to 2015.site.moscow

And of course, not only appeals to the domain index, but also any other, for example, to http://2015.site.ru/images/pic.gif converted to http://2015.site.moscow/images/pic.gif

However, when accessing the main site site.ru should not be redirected to site.moscow Can you please tell me how to register this in. Htaccess?

  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

2 answers 2

just a classic. copied from answer :

rewriteengine on rewritecond %{HTTP_HOST} ^a\.example\.com rewriterule ^(.*)$ http://b.example.com/$1 [r,l] 

additional Information: http://wiki.apache.org/httpd/WhenNotToUseRewrite

    If limited only by the HTTP , then the necessary redirect can be written, for example, like this:

     RewriteEngine On RewriteCond %{HTTP_HOST} ^2015\.site\.ru$ RewriteRule ^(.*)$ http://2015.site.moskow/$1 [R=301,L] 

    If you need to redirect the HTTPS protocol in the same way, the rule will be a bit more complicated:

     RewriteEngine On RewriteCond %{HTTP_HOST} ^2015\.site\.ru$ [NC] RewriteCond %{HTTPS} off RewriteRule ^(.*)$ http://2015.site.moskow/$1 [R=301,L] RewriteCond %{HTTP_HOST} ^2015\.site\.ru$ [NC] RewriteCond %{HTTPS} on RewriteRule ^(.*)$ https://2015.site.moskow/$1 [R=301,L] 

    This rule is understandable, but redundant and made too straightforward. You can optimize it by removing duplicate elements. The result is the following:

     RewriteEngine On RewriteCond %{HTTPS} off RewriteRule ^(.*)$ - [env=protocol:http] RewriteCond %{HTTPS} on RewriteRule ^(.*)$ - [env=protocol:https] RewriteCond %{HTTP_HOST} ^2015\.site\.ru$ [NC] RewriteRule ^(.*)$ %{ENV:protocol}://2015.site.moskow/$1 [R=301,L] 

    Interestingly, I was unable to find an online .htaccess file tester that understands the latter option. But live Apache handled it correctly.