Apache 通过.htaccess将http重定向到https不工作

Apache 通过.htaccess将http重定向到https不工作,apache,.htaccess,redirect,ssl,https,Apache,.htaccess,Redirect,Ssl,Https,我在这里看到了一些类似的问题,但似乎没有答案。我只是想不通。这是我的密码: # enable basic rewriting RewriteEngine on # enable symbolic links Options +FollowSymLinks # Primary Domain - Force the use of "https" RewriteCond %{HTTP_HOST} !^www\. RewriteRule ^(.*)$ https://www.%{HTTP_HOST

我在这里看到了一些类似的问题,但似乎没有答案。我只是想不通。这是我的密码:

# enable basic rewriting

RewriteEngine on

# enable symbolic links
Options +FollowSymLinks

# Primary Domain - Force the use of "https"
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]

# Primary Domain - Force the use of "www"
RewriteCond %{http_host} ^example.org [nc]
RewriteRule ^(.*)$ https://www.example.org/$1 [r=301,nc]

# Primary Domain - .com to .org
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(.*)$ https://www.example.org/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ https://www.example.org/$1 [R=301,L]

# Primary Domain - .net to .org
RewriteCond %{HTTP_HOST} ^example.net$ [NC]
RewriteRule ^(.*)$ https://www.example.org/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www.example.net$ [NC]
RewriteRule ^(.*)$ https://www.example.org/$1 [R=301,L]
所以每当我去
www.example.org
http://www.example.org
它不会重定向。这就是让我沮丧的地方。如果我转到example.org或任何其他重定向,它仍然可以工作。我是不是搞错了


还有,如果它写着“HTTP_HOST”,我应该保持原样吗?像我用我的http主机替换它吗?

Amie如果你想强制
www
https
两者,你可以检查是否没有www。或者检查
https
是否
上,然后重定向。所以无论哪种方式,它都会重定向。例如

# Primary Domain - Force the use of "https" and www
RewriteCond %{HTTP_HOST} !^www\. [OR]
RewriteCond %{HTTPS} !^on$
RewriteRule ^(.*)$ https://www.mydomain.org/$1 [R=301,L]
将mydomain.org替换为您的域

然后你可以有一个像这样的htaccess文件。我采用了最后两条规则并将它们组合在一起,这样您就有了一个更小的htaccess文件

# enable basic rewriting
RewriteEngine on

# enable symbolic links
Options +FollowSymLinks

# Primary Domain - Force the use of "https" and www
RewriteCond %{HTTP_HOST} !^www\. [OR] 
RewriteCond %{HTTPS} !^on$
RewriteRule ^(.*)$ https://www.mydomain.org/$1 [R=301,L]

# Primary Domain - .com to .org Or .net to .org
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.(net|com)$ [NC]
RewriteRule ^(.*)$ https://www.mydomain.org/$1 [R=301,L]

还有,如果它写着“HTTP_HOST”,我应该保持原样吗?我也喜欢 用我的http主机替换它

%{HTTP_HOST}
是一个变量,包含从浏览器发送的带有HTTP头的主机。因此,如果有人请求
http://mydomain.org/somepage.php
该变量
%{HTTP\u HOST}
将包含
mydomain.org
。你不能改变它。您可以使用它来匹配事物或设置规则中的条件。希望这能回答你的问题