简体   繁体   English

在 htaccess 重定向中强制使用非 www 和 HTTPS 会导致重定向过多

[英]Force non-www and HTTPS in htaccess redirect results in too many redirects

I'm trying to force non-www https in htaccess, but every example I find throws the error "too many redirects"我试图在 htaccess 中强制使用非 www https,但我发现的每个示例都会抛出错误“太多重定向”

I want to redirect:我想重定向:

  • http://www.example.com
  • http://example.com
  • https://www.example.com

to:到:

  • https://example.com

The best explained solution I've found is here:我找到的最好的解释解决方案是在这里:
https://simonecarletti.com/blog/2016/08/redirect-domain-http-https-www-apache/ https://simonecarletti.com/blog/2016/08/redirect-domain-http-https-www-apache/

...Which gives the following example: ...给出了以下示例:

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [L,NE,R=301]

Thanks to the explanation I understand what it's doing, but I'm still getting the same error - too many redirects.多亏了解释,我明白了它在做什么,但我仍然遇到同样的错误——重定向太多。

The best I have been able to do is:我能做的最好的是:

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

...But of course that doesn't redirect http://example.com . ...但是当然不会重定向http://example.com

I have no redirects setup in httpd.conf , and None setup in Plesk.我在httpd.conf中没有设置重定向,在 Plesk 中也没有设置。 I'm on CentOS 6.8, Apache 2.2.15, Plesk 12.5.我在 CentOS 6.8、Apache 2.2.15、Plesk 12.5 上。

What could be causing the issue?是什么导致了这个问题?

Adding the RewriteCond and RewriteRule you specify gives me ?off_example.com when using http:// or https:// .使用 http:// 或https://时,添加您指定的RewriteCondRewriteRule会给我?off_example.com Is that expected?这是预期的吗?

No, that's not the expected result - that will be the problem.不,这不是预期的结果 - 这将是问题所在。 HTTPS should be on when accessed over https://... .通过https://...访问时应on HTTPS If the HTTPS server variable is never set to "on" (or rather, never changes from "off") then your RewriteRule will result in a redirect loop.如果HTTPS服务器变量从未设置为“on”(或者更确切地说,从未从“off”更改),那么您的RewriteRule将导致重定向循环。

This suggests that the "Let's Encrypt addon in Plesk" is implemented via some kind of "proxy" (front-end) server?这表明“让我们在 Plesk 中加密插件”是通过某种“代理”(前端)服务器实现的? Your application server still responds over unencrypted HTTP to the proxy and the clients connection to the proxy is encrypted over HTTPS.您的应用程序服务器仍然通过未加密的 HTTP 响应代理,并且客户端与代理的连接通过 HTTPS 加密。 At least, that's what it looks like - but your host should be able to confirm this.至少,它看起来是这样的——但你的主人应该能够确认这一点。

If this is the case then the proxy usually sets additional HTTP request headers with details about the client's connection.如果是这种情况,那么代理通常会设置额外的 HTTP 请求标头,其中包含有关客户端连接的详细信息。 You should be able to examine the request headers that your application sees, but it is common for the X-Forwarded-Proto header to be set with the protocol that is being used ("http" or "https").您应该能够检查您的应用程序看到的请求标头,但X-Forwarded-Proto标头通常设置为正在使用的协议(“http”或“https”)。 If this is the case then you can probably change your existing directives to something like the following instead:如果是这种情况,那么您可能可以将现有指令更改为如下所示:

RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [L,NE,R=301]

Another, less common method is for the (front-end) server to set an HTTPS environment variable (possibly provided by mod_ssl ?) instead - note that this is different to the similarly named HTTPS server variable , as mentioned above.另一种不太常见的方法是让(前端)服务器设置一个HTTPS环境变量(可能由 mod_ssl 提供?)——请注意,这与上面提到的名称相似的HTTPS服务器变量不同。 This environment variable is accessed using the syntax %{ENV:HTTPS} in the mod_rewrite directive and, if set, contains the same value as would otherwise be provided by the HTTPS server variable .使用 mod_rewrite 指令中的语法%{ENV:HTTPS}访问此环境变量,如果设置,则包含与HTTPS服务器变量提供的值相同的值。 For example:例如:

RewriteEngine On
RewriteCond %{ENV:HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [L,NE,R=301]

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

相关问题 htaccess 强制 https 并将 www 重定向到非 www,但没有其他子域 - htaccess force https and redirect www to non-www, but no other subdomains htaccess重定向:WWW + HTTPS:// WWW + HTTP:// ---> HTTPS:// non-www - Htaccess Redirect: WWW + HTTPS://WWW + HTTP:// ---> HTTPS://non-www .htaccess非www到www和http到https重定向 - .htaccess non-www to www and http to https redirects htaccess将https和www强制为非www,但仅子域https - htaccess force https & www to non-www, but subdomain only https 将www和非www重定向到https www .htaccess不起作用 - redirect www and non-www to https www .htaccess not working 如何使用 .htaccess 将 HTTPS 非 www 重定向到 HTTPS www? - How to redirect HTTPS non-www to HTTPS www using .htaccess? 如何通过语言功能将 htaccess 重定向与 www 合并到非 www 和 http 到 https? 浏览器返回:err_too_many_redirects - How to merge htaccess redirect by language function with www to non www and http to https? browsers return: err_too_many_redirects Apache + Passenger非www到www =>重定向太多 - Apache + Passenger non-www to www => too many redirects 使用.htaccess将所有非www重定向到www和https的www - Redirect all non-www to www for both http and https with .htaccess 使用apache和.htaccess将www重定向到非www https - redirect www to non-www https with apache & .htaccess
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM