Answer the question
In order to leave comments, you need to log in
How to make a redirect in .htaccess from several domains and subdomains to the corresponding domain/subdomains?
There was a need for a rather non-standard (for me) 301 domain redirect in .htaccess. In the case when there is a redirect from several domains (for example, domain1.ru, sub1.domain2.ru, sub2.domain1.ru) to a specific domain.ru domain while saving the address of the required page, I use the following code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^domain\.ru$ [NC]
RewriteRule ^(.*)$ http://domain.ru/$1 [L,R=301]
</IfModule>
Answer the question
In order to leave comments, you need to log in
I read about regular expressions in htaccess and came to this solution, maybe someone else will come in handy:
RewriteCond %{HTTP_HOST} !^domain.ru$|^(.*\.)domain\.ru$
RewriteCond %{HTTP_HOST} ^(.*\.)?(.*\.).*\..*$|^(.*).(.*)$
RewriteRule ^(.*)$ http://%2domain\.ru/$1 [L,R=301]
Something like this (didn't check)
RewriteCond %{HTTP_HOST} !([^\.]+)\.domain\.ru$
RewriteRule ^(.*)$ http://%1.domain.ru/$1 [R=301,L]
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question