Z
Z
zmrzjke2020-01-30 11:56:32
Apache HTTP Server
zmrzjke, 2020-01-30 11:56:32

How to make a 301 redirect for a site with subdomains?

Hello.

In general, the site is moving to a new domain and all requests that refer to the old domain should be redirected to the new domain, including subdomains.

For example, for the main domain, the rule seems to be something like this:

RewriteCond %{HTTP_HOST} ^old-site\.ru$ [NC]
RewriteRule ^(.*)$ http://www.new-site.ru/$1 [R=301,L]


But it will not work, of course, on subdomains. It is
necessary that when requesting, for example, subdomain.old-site.ru, there is a redirect to subdomain.new-site.ru
There are about 200 subdomains and write individually for each, I think it is not a rational solution at all.

I would like some universal rule. Who can help with ideas! Thanks to.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dodo512, 2020-01-30
@zmrzjke

RewriteCond %{HTTP_HOST} ^old-site\.ru$ [NC]
RewriteRule ^(.*)$ http://www.new-site.ru/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^(?:www\.|)(.+)\.old-site\.ru$ [NC]
RewriteRule ^(.*)$ http://%1.new-site.ru/$1 [R=301,L]

#
        old-site.ru  ->  www.new-site.ru
    www.old-site.ru  ->  www.new-site.ru
    one.old-site.ru  ->  one.new-site.ru
www.one.old-site.ru  ->  one.new-site.ru
    two.old-site.ru  ->  two.new-site.ru
www.two.old-site.ru  ->  two.new-site.ru

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question