.htaccess too many redirections in Safari

2019-05-25 01:56发布

问题:

I have htaccess file consisting of

RewriteEngine on
RewriteBase /

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

# Turn SSL on for 
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/m/log
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

# Turn SSL off 
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/m/log
RewriteCond %{REQUEST_URI} !^/m/stil 
RewriteCond %{REQUEST_URI} !^/m/slike 
RewriteCond %{REQUEST_URI} !^/m/slikce 
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]

It runs fine on basically any other device except iPhone. How to fix it?

It happens only on /m/log while using iPhone6, it's fine on iPhone 4. It happens regardless of whether the site is accessed via HTTP or HTTPS, or via www or the bare domain.

回答1:

Having had a quick look at the network traffic on the site itself, it would seem that when serving https://example.com‌/m/log (ie. the "secure" page), you have a few images (including the favicon.ico) that are being redirected back to http. This results in "mixed content" and an insecure connection. Some browsers might throw up a warning or simply not display the usual "padlock". (Google Chrome omits the green padlock but only highlights the "mixed content" issue when examining​ the security information.) This needs to be corrected.

If the user-agent (ie. Safari on iPhone 6) attempts to "correct this" (to make it secure) by re-requesting the secure resource then you could well end up with "too many redirects" (a redirect loop).

The images (URLs) in question.

  • /slikce/smile.png (Curiously, this also returns an X-Pad: avoid browser bug response header?)
  • /log/slike/1471075891/tm_13950809_10209500982108999_1789026171_o.jpg
  • /favicon.ico

All the above URLs appear on the "secure" HTML page, however, they get 301 redirected back to HTTP following the rules in your .htaccess file.

(It also appears that the HTTPS requests are handled by Apache, but the HTTP requests are handled by Nginx?)

Incidentally, I'm also seeing this error while testing your site in Safari on iOS 9 (iPad)...

Safari cannot open the page because too many redirects occurred.



回答2:

I fixed it by using following method used in redirection

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.