将 http/https 子域(邮件)重定向到 .htaccess 中的 https 主域

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

我知道我的问题被问过很多次,但不幸的是我找不到答案。可能是因为我没有将 .htaccess 文件放在正确的位置。 我想要的是重定向http://mail.example.com , https://mail.example.com to https://example.com。 我还想重定向 https://www.example.com , http://example.com, http://www.example.com to https://example.com。我通过在 cpanel public_html 的 .htaccees 文件中添加以下代码来完成这项工作并且它有效。

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

我使用了这个 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 中的以下代码。可能是 https 的问题。

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

感谢任何帮助。 p.s。我的网站是用 joomla 设计的,在 cpanel 和 joomla 设置中没有重定向。

我认为一切都是正确的,只是顺序问题:

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]

在你的主 .htaccess 文件中