将所有非www重定向到www,将所有http重定向到https,并排除子域

时间:2020-02-07 20:08:28

标签: apache .htaccess

我正在尝试重定向

example.com到www.example.com 和 http://example.comehttps://www.example.com

但是,如果用户键入

admin.example.com,http://admin.example.comhttps://admin.example.com 它不应重定向到www.admin.example.com

我可以使用什么.htaccess?

1 个答案:

答案 0 :(得分:0)

您可以在网站根目录.htaccess中使用此规则:

RewriteEngine On

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