.htaccess文件未重定向http:// www。到https:// www [英] .htaccess file not redirecting http://www. to https://www

查看:84
本文介绍了.htaccess文件未重定向http:// www。到https:// www的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个.htaccess文件,用于将所有网站流量重定向到 https:// www。

I have made a .htaccess file to redirect all website traffic to https://www..

这是我完整的.htaccess文件:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

以下重定向完全按预期工作:

The below redirects work exactly as expected:

http://example.com -> https://www.example.com
https://example.com -> https://www.example.com
https://www.example.com -> https://www.example.com

例外:

http://www.example.com -> http://www.example.com

如上所示,如果您转到 http:// www。不会重定向到HTTPS版本。

As shown above, if you go to http://www. it doesn't redirect to the HTTPS version.

有人可以帮助我理解为什么其他重定向是工作正常,但不是吗?

Can anyone help me understand why the other redirects are working fine, but that one is not?

其他说明:我看过StackOverflow上的许多文章,但大多数解决方案都以重定向循环结尾错误。

推荐答案

与123-Reg(我的托管服务提供商)联系后,他们提交了此解决方案,效果很好:

After contacting 123-Reg (my hosting provider), they submitted this solution, which works perfectly:

RewriteEngine on

RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301,NC]

RewriteCond %{ENV:HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

基本上,他们已将脚本设置为执行两个任务:将域更改为 WWW ,如果尚未将其更改为 HTTPS 。此外,他们使用了 ENV:HTTPS ,这与文档中的内容( ENV:SSL )不同。

Basically they have set the script to do two tasks: Change domain to WWW, if it isn't already, THEN change to HTTPS. Also, they used ENV:HTTPS, which is different to what was found in their documentation (ENV:SSL).

很高兴能够进行这种排序,也许这可以使用123-Reg Hosting帮助其他人。

Glad to have to this sorted, and maybe this will help out others using 123-Reg Hosting.

这篇关于.htaccess文件未重定向http:// www。到https:// www的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆