htaccess允许http和https - 更改为仅允许/重定向到https

时间:2016-06-02 20:57:40

标签: .htaccess

我继承了以下htaccess,并且在将其始终重定向到https时遇到问题。

目前它同时允许http://wwwhttps://www,但我希望它始终重定向到https://www(子域应始终重定向到https://subdomain.example.com

到目前为止我所有的尝试都造成了500次错误,所以我显然错过了一些东西。

AddDefaultCharset UTF-8

RewriteEngine On

Options +FollowSymLinks

RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS}s on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example.com$ [NC]
RewriteCond %{REQUEST_URI} !sub/
RewriteRule (.*) /sub/$1 [L]

1 个答案:

答案 0 :(得分:1)

您可以将规则调整为:

AddDefaultCharset UTF-8
Options +FollowSymLinks
RewriteEngine On

# handle sub-domains
RewriteCond %{HTTP_HOST} ^(?!www\.)[^.]+\.example\.com$ [NC]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

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

RewriteCond %{HTTP_HOST} ^(?!www\.)[^.]+\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/sub/ [NC]
RewriteRule (.*) sub/$1 [L]