简体   繁体   English

通过.htaccess将http重定向到https无效

[英]Redirect http to https via .htaccess not working

I've seen a few questions like this on here, but no answer seems to work. 我在这里看到过这样的几个问题,但似乎没有答案可行。 I just can't seem to figure this out. 我似乎无法弄清楚这一点。 Here is my code: 这是我的代码:

# 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]

So whenever I go to www.example.org or http://www.example.org it will NOT redirect. 因此,每当我访问www.example.orghttp://www.example.org它都不会重定向。 This is what is frustrating me. 这让我感到沮丧。 If I go to example.org or any of the other redirects, it works though. 如果我去example.org或任何其他重定向,它可以工作。 Am I setting this up wrong? 我设置错了吗?

Also, where it says "HTTP_HOST", do I leave it as is? 此外,如果它显示“HTTP_HOST”,我会保留原样吗? Like do I replace it with my http host? 我喜欢用我的http主机替换它吗?

Amie if you want to force www and https both you can check for the absence of www. Amie如果你想强制使用wwwhttps ,你可以检查是否缺少www。 or check if https is not On then redirect. 或检查https On然后重定向。 So either way it will redirect. 所以无论哪种方式,它都会重定向。 example. 例。

# 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]

Replace mydomain.org with your domain. 将mydomain.org替换为您的域名。

Then you can have a htaccess file like this. 然后你可以有这样的htaccess文件。 I took the last two rules and combined them so you have a smaller htaccess file. 我采用了最后两条规则并将它们组合在一起,因此您有一个较小的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]

Also, where it says "HTTP_HOST", do I leave it as is? 此外,如果它显示“HTTP_HOST”,我会保留原样吗? Like do I replace it with my http host? 我喜欢用我的http主机替换它吗?

%{HTTP_HOST} is a variable that contains the host sent with Http headers from the browser. %{HTTP_HOST}是一个变量,包含从浏览器使用Http头发送的主机。 So if someone requests http://mydomain.org/somepage.php that variable %{HTTP_HOST} will contain mydomain.org . 因此,如果有人请求http://mydomain.org/somepage.php ,变量%{HTTP_HOST}将包含mydomain.org You don't change it. 你不要改变它。 You can use it to match things or set conditions like in the rules. 您可以使用它来匹配事物或设置条件中的条件。 Hope that answers your question. 希望这能回答你的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM