Https causes too many redirects on Safari

2019-02-28 05:17发布

问题:

I recently started redirecting my domain to https and I am getting a "too many redirects" error on Safari, especially on mobile. Everything works fine on any computers and in any other browsers.

Here's how I've done my redirects.

for main domain

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

I also disabled Cloudflare because it was causing issues with my SSL and I fixed some issues with DNS propagation.

EDIT:

It affects all safari browsers except for computers and deleting the cache doesn't solve the issue. I only have one domain, but a few subdomains.

I do have other directives in .htaccess:

RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^folder\/?(.*)$ "http\:\/\/example\.com\/all\-categories\/folder\/$1" [R=301,L]

EDIT: SOLVED

Changing host solved this problem for me.

回答1:

RewriteRule ^folder\/?(.*)$ "http\:\/\/example\.com\/all\-categories\/folder\/$1" [R=301,L]

Since you are redirecting everything to https, you should also be doing this in the above redirect. Otherwise you are going to end up with additional (unnecessary) redirects. So this should be rewritten as:

RewriteRule ^folder/?(.*) https://example.com/all-categories/folder/$1 [R=301,L]

I have also tidied up the regex and substitution (this looks like something cPanel would generate?). There is no need for all the backslash escapes (and double quotes) in the RewriteRule substitution. And the backslash escape in the RewriteRule pattern is also superfluous.

However, I doubt whether this alone is enough to satisfy Safari (mobile). (?)



回答2:

As you're using CloudFlare's Flexible SSL; the web server might not be able to correctly identify when the connection should be over SSL and when it shouldn't.

As such you should attempt to install Mod_CloudFlare on your webserver to resolve this, if you can't do this, you can instead set your SSL environment variable from the X-Forwarded-Proto header in your Apache .htaccess:

SetEnvIf X-Forwarded-Proto https HTTPS=on



回答3:

I fixed it by using following method

RewriteCond %{SERVER_PORT}  !^443$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteCond %{HTTPS} off

is causing the error. use

RewriteCond %{SERVER_PORT} !^443$

to fix it.