问题原文
原帖链接 · authentictech · CC BY-SA 4.0
I have been asked to help with an OpenCart 1.5.6.1 installation on shared hosting with some redirection requirements.
Some of the redirects were already present and I'm not sure if they are required for OpenCart.
The new requirement is to redirect from HTTP to HTTPS.
When I add a command to redirect from HTTP to HTTPS the browser reports an error that redirects are not working correctly. This command has always worked on other sites before. I am guessing that there is a conflict with one or more of the other redirect commands.
These are the existing redirect commands with the comments on what they do:
# Redirect oldexample.co.uk to example.co.uk
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)oldexample\.co.uk$ [NC]
RewriteRule ^ https://www.example.co.uk%{REQUEST_URI} [L,R=301]
# If your opencart installation does not run on the main web folder make sure you folder it does run in ie. / becomes /shop/
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=feed/google_base [L]
RewriteRule ^download/(.*) /index.php?route=error/not_found [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
The directive I add which causes redirects to break is:
#Redirect HTTP to HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Why does this not work and how can get all of these redirect requirements to work together?
Additional requested information:
The result of curl --head http://example.co.uk
HTTP/1.1 301 Moved Permanently
date: Mon, 29 Nov 2021 18:17:10 GMT
server: Apache/2.4.51 (Unix)
location: https://www.example.co.uk/
content-type: text/html; charset=iso-8859-1
The result of curl --head http://www.example.co.uk
HTTP/1.1 301 Moved Permanently
date: Mon, 29 Nov 2021 19:20:47 GMT
server: Apache/2.4.51 (Unix)
location: https://www.example.co.uk/
content-type: text/html; charset=iso-8859-1
The result of curl --head https://www.example.co.uk is
HTTP/2 301
date: Mon, 29 Nov 2021 19:20:54 GMT
server: Apache/2.4.51 (Unix)
location: https://www.example.co.uk/
content-type: text/html; charset=iso-8859-1
This I suppose is where a redirection loop occurs.
Output of print_r( $_SERVER, true) HTTP_ indices:
[HTTPS] => on
[HTTP_USER_AGENT] => Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:94.0) Gecko/20100101 Firefox/94.0
[HTTP_ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8
[HTTP_ACCEPT_LANGUAGE] => en-GB,en;q=0.5
[HTTP_DNT] => 1
[HTTP_ACCEPT_ENCODING] => gzip, deflate, br
[HTTP_UPGRADE_INSECURE_REQUESTS] => 1
[HTTP_SEC_FETCH_DEST] => document
[HTTP_SEC_FETCH_MODE] => navigate
[HTTP_SEC_FETCH_SITE] => none
[HTTP_SEC_FETCH_USER] => ?1
[HTTP_CACHE_CONTROL] => max-age=0
[HTTP_TE] => trailers
[HTTP_HOST] => www.example.co.uk (changed)
[HTTP_COOKIE] => language=en; currency=GBP; PHPSESSID=(removed)
[HTTP_X_PROTO] => SSL
[HTTP_CONNECTION] => close
已采纳解答
原帖链接 · MrWhite · CC BY-SA 4.0
[HTTP_X_PROTO] => SSL
The presence of the X-Proto HTTP request header would seem to imply you are behind some kind of proxy, that is perhaps managing the secure connection. Although the use of X-Proto is (very) non-standard, particularly with the given value.
Try the following immediately after your non-www to www redirect:
RewriteCond %{HTTP:X-Proto} !=SSL
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
You will need to clear your browser cache and test first with a 302 (temporary) redirect to avoid caching issues.
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
