htaccess没有重定向到https

时间:2018-11-26 09:08:35

标签: apache .htaccess redirect

我正在尝试在根目录中创建一个htaccess文件,该文件也会影响子域目录(子域从根目录中的目录开始工作)。

首先,如果我的根目录中有一个htaccess文件,这可以工作吗? (它是否适用于浏览器中的子域(因为它们实际上只是根目录)?)。

我遇到的问题是,当我尝试访问http://demo.example.com时,并没有使用它重定向到https://demo.example.com,而只是使用http://。尝试查看http://www.example.com(不会重定向到https)时也是如此。

这是我目前的整个htaccess文件。对我来说似乎很肿。有什么办法可以将所有内容重定向到https,而不管子域/根是什么?

<IfModule mod_rewrite.c>
RewriteEngine On

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

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

1 个答案:

答案 0 :(得分:0)

您可以在网站根.htaccess中尝试以下规则吗?

RewriteEngine On

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

# redirect everything to https
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

在新浏览器中对其进行测试,或者在测试之前完全清除浏览器缓存。

相关问题