简体   繁体   English

.htaccess文件没有重定向http:// www。 到https:// www

[英].htaccess file not redirecting http://www. to https://www

I have made a .htaccess file to redirect all website traffic to https://www. 我制作了一个.htaccess文件,将所有网站流量重定向到https://www. .

This is my complete .htaccess file: 这是我完整的.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

Except: 除了:

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

As shown above, if you go to http://www. 如上所示,如果你去http://www. it doesn't redirect to the HTTPS version. 它不会重定向到HTTPS版本。

Can anyone help me understand why the other redirects are working fine, but that one is not? 谁能帮助我理解为什么其他重定向工作正常,但那个不是?

Additional Notes: I have looked at a number of posts on StackOverflow, but most of their solutions end in redirect loop errors. 附加说明: 我已经查看了StackOverflow上的一些帖子,但他们的大部分解决方案都以重定向循环错误结束。

After contacting 123-Reg (my hosting provider), they submitted this solution, which works perfectly: 在联系123-Reg(我的托管服务提供商)后,他们提交了此解决方案,该解决方案完美运行:

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]

Basically they have set the script to do two tasks: Change domain to WWW , if it isn't already, THEN change to HTTPS . 基本上他们已经将脚本设置为执行两项任务:将域更改为WWW ,如果尚未更改,则更改为HTTPS Also, they used ENV:HTTPS , which is different to what was found in their documentation ( ENV:SSL ). 此外,他们使用ENV:HTTPS ,这与他们的文档( ENV:SSL )中的内容不同。

Glad to have to this sorted, and maybe this will help out others using 123-Reg Hosting. 很高兴有这个排序,也许这将帮助其他人使用123-Reg Hosting。

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

As you have found, this won't redirect when requesting the canonical hostname (ie. www.example.com ) regardless of whether it is HTTP or HTTPS. 如您www.example.com ,无论是HTTP还是HTTPS,在请求规范主机名(即www.example.com )时都不会重定向。

You need to change this to something like: 您需要将其更改为:

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

This will trigger a redirect to the canonical URL if HTTPS is "off" or it's not the canonical hostname. 如果HTTPS“关闭” 或者它不是规范主机名,这将触发重定向到规范URL。

...but most of their solutions end in redirect loop errors. ...但他们的大多数解决方案都以重定向循环错误结束。

If you are behind a proxy (eg. CloudFlare) that is managing your SSL cert then this could still result in a redirect loop because the connection between you and the proxy might be HTTP, not HTTPS. 如果您在管理SSL证书的代理(例如CloudFlare)后面,那么这仍然可能导致重定向循环,因为您和代理之间的连接可能是HTTP,而不是HTTPS。 This would mean that your server only serves content over HTTP, not HTTPS. 这意味着您的服务器仅通过HTTP提供内容,而不是HTTPS。 If this is the case then there are additional headers that can be checked on the request (eg. X-Forwarded-Proto ) or set a "page rule" in the case of CloudFlare (Flexible SSL - free service). 如果是这种情况,则可以在请求中检查其他标头(例如, X-Forwarded-Proto )或在CloudFlare(灵活SSL - 免费服务)的情况下设置“页面规则”。


UPDATE#1: 123-Reg provide a help document regarding SSL . 更新#1: 123-Reg提供有关SSL的帮助文档 It seems they set an SSL environment variable when "the connection is SSL-secured". 当“连接受SSL保护”时,它们似乎设置了SSL环境变量。 This would mean that you could potentially do something like the following instead: 这意味着您可能会执行以下操作:

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

This is, however, non-standard and "unique" to 123-Reg. 然而,这是123-Reg的非标准和“独特”。 ( Aside: The PHP code suggestion in the 123-Reg linked document is not the recommended way to check the environment variable, as this would result in an E_NOTICE if the variable is not set!?) 旁白: 123-Reg链接文档中的PHP代码建议不是检查环境变量的推荐方法,因为如果未设置变量,这将导致E_NOTICE!?)

You should also ensure your browser cache is cleared before testing. 您还应确保在测试之前清除浏览器缓存。

UPDATE#2: To help with debugging... to find out what values are being returned, you could assign some Apache values to environment variables and check the values of these in your server-side script (eg. PHP?). 更新#2:为了帮助调试...找出返回的值,可以将一些Apache值分配给环境变量,并在服务器端脚本中检查这些值(例如PHP?)。 For example: 例如:

RewriteCond %{HTTPS} (.*)
RewriteRule ^ - [E=APACHE_HTTPS:%1]

# You don't really need this, but for completeness...
RewriteCond %{ENV:SSL} (.*)
RewriteRule ^ - [E=APACHE_SSL:%1]

RewriteCond %{HTTP:X-Forwarded-Proto} (.*)
RewriteRule ^ - [E=APACHE_PROTO:%1]

Then check the environment variables APACHE_HTTPS , APACHE_SSL and APACHE_PROTO in your server-side script. 然后检查服务器端脚本中的环境变量APACHE_HTTPSAPACHE_SSLAPACHE_PROTO (eg. in PHP, use the getenv() function.) (例如,在PHP中,使用getenv()函数。)

After lots of issues with 123 Reg and redirecting all versions of pages to single relevant https page with Wordpress, this is what has worked for me across multiple sites now and has proven effective in terms of SEO. 经过123 Reg的许多问题并使用Wordpress将所有版本的页面重定向到单个相关的https页面,这就是我现在在多个站点上工作的东西,并且已经证明在SEO方面是有效的。 Hope it helps! 希望能帮助到你!

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{ENV:HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

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

相关问题 htaccess将http://(www。)old.com和https://(www。)old.com重定向到https:// new - htaccess redirecting http://(www.)old.com and https://(www.)old.com to https://new htaccess http到带有www的https。 不重定向子域 - htaccess http to https with www. Without redirecting sub domain 将所有非www,www,http://请求重定向到https:// www。 使用.htaccess文件 - Redirect all non-www, www, http:// requests to https://www. using .htaccess file htaccess强制https和www。 在域上&http在子域上没有www - htaccess force https & www. on domain & http no www on subdomain Getting .htaccess redirects from one domain to another to work for WWW ( both http://www. and https://www.), HTTP and HTTPS - Getting .htaccess redirects from one domain to another to work for WWW ( both http://www. and https://www.), HTTP and HTTPS .htaccess重定向到https和www。 如果启用 - .htaccess redirect to https and www. if enabled htaccess:使用https:// www。 有例外 - Htaccess: Use https://www. with exceptions 在Apache上将http重定向到https和www到非www - redirecting http to https and www to non www on apache htaccess重定向https:// www。 (https://www.example.com)到非www(https://example.com) - htaccess redirect https://www. (https://www.example.com) to non www (https://example.com) htaccess文件www。 重定向问题 - htaccess file www. redirect issue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM