.htaccess error - ERR_TOO_MANY_REDIRECTS

我的未来我决定 提交于 2019-11-30 23:23:12
RewriteCond %{HTTPS} on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

Yes, this will create a redirect loop. The logic is wrong. What this says is... if HTTPS is "on" then redirect to HTTPS. You should be checking if HTTPS is "off" (or "not on", ie. !on).

(By removing the spaces between the arguments you likely created a rewrite loop, hence the 500 error. Spaces are delimiters in Apache config files.)

Try something like the following instead:

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

This handles both the HTTPS and www canonical redirects. You don't need the first rule. You don't need the <IfModule> container either.

Change the 302 to 301 only when you are sure it's working OK.

Make sure you've cleared your browser cache before testing. 301s get cached hard by the browser.


UPDATE: If this still gives you the same error (a redirect loop) then it's possible that your SSL is managed by a front-end proxy, not your application server. If this is the case then you won't be able to use the HTTPS server variable. See these related questions:


It seems that in this case, ENV:HTTPS (an environment variable) needed to be used in place of HTTPS (Apache server variable). Note, however, that this is non-standard / server specific, as it implies a front-end proxy is being used to manage the SSL.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!