The .htaccess contains the instructions for redirecting all variants of writing the site address to the main domain https://mysite.ru .:

 RewriteCond %{HTTPS} off [OR] RewriteCond %{HTTP_HOST} ^www\. RewriteRule (.*) https://mysite.ru/$1 [L,R=301] 

I would also like to implement redirecting subdomains to the appropriate folder. The instruction works well on ordinary http-domains:

  RewriteCond %{ENV:REDIRECT_VHOST} ^$ RewriteCond %{HTTP_HOST} ^(.+)\.mysite\.ru$ RewriteRule ^(.*) %1/$1 [L,E=VHOST:1] 

However, in my case, the two redirects naturally conflict, and subdomains do not work. I can’t figure out how to combine them together - (the “before” or “after” permutation, of course, does not work). For example, the addresses: https://sub.mysite.ru , http://sub.mysite.ru , www.sub.mysite.ru redirected as it should be to https://mysite.ru/sub .

The following instruction does not work :

 RewriteEngine on RewriteBase / # переадресация субдоменов RewriteCond %{ENV:REDIRECT_VHOST} ^$ RewriteCond %{HTTP_HOST} ^(.+)\.mysite\.ru$ RewriteRule ^(.*) %1/$1 [L,E=VHOST:1] # переадресация на https: RewriteCond %{HTTPS} off [OR] RewriteCond %{HTTP_HOST} ^www\. RewriteRule (.*) https://mysite.ru/$1 [L,R=301] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule .* index.php [L,NS] 

    1 answer 1

    The answer is found, I will be glad if it is useful to someone else: http: , www redirection and without specifying the protocol to https: Only after this, the subdomains are checked and redirected:

     RewriteEngine On RewriteCond %{HTTPS} off [OR] RewriteCond %{HTTP_HOST} ^www\. [NC] RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC] RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301] RewriteCond %{ENV:REDIRECT_VHOST} ^$ RewriteCond %{HTTP_HOST} ^(www\.)?(.+)\.mysite\.ru$ RewriteRule ^(.*) %2/$1 [L,E=VHOST:1] 

    Tested on a real site.