Search code examples
.htaccesshttp-redirecthttpssubdomainno-www

redirect http/https subdomain (mail) to https main domain in .httaccess


I know my question is asked many times but unfortunately I could't find my answer. Maybe because I don't put the .htaccess file in the correct place. What I want is to redirect http://mail.example.com , https://mail.example.com to https://example.com. I also wanted to redirect https://www.example.com , http://example.com, http://www.example.com to https://example.com. I did this job with adding below code in .htaccees file in public_html in cpanel and it works.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\. [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$
RewriteRule ^ https://%1%{REQUEST_URI} [NE,L,R=301]

I used below code from this link and put .htaccess file in mail folder. but when I type http://mail.example.com it redirects to https://mail.example.com and https://mail.example.com also doesn't go to http://example.com. Maybe the problem is with https.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^mail\.mysit\.com  [NC]
RewriteRule ^(.*) https://example.com/$1 [L,R=301]

Any help is appreciated. p.s. My site is designed with joomla and there is no redirection in cpanel and joomla settings.


Solution

  • I think everything is correct, and that 'it's only a matter of order:

    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} ^mail\.example\.com  [NC]
    RewriteRule ^ https://example.com%{REQUEST_URI} [NE,L,R=301]
    
    RewriteCond %{HTTP_HOST} ^www\. [OR]
    RewriteCond %{HTTPS} off
    RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$
    RewriteRule ^ https://%1%{REQUEST_URI} [NE,L,R=301]
    

    In you main .htaccess file