问题原文
原帖链接 · z3nth10n · CC BY-SA 4.0
Well, I'm trying to accomplish this task by using this .htaccess:
Options +FollowSymLinks
RewriteEngine On
# HTTPS (not working: ERR_TOO_MANY_REDIRECTS)
# RewriteCond %{HTTPS} off
# RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
# Capture Global Request
RewriteCond %{THE_REQUEST} \S+\s+(\S*)
RewriteRule ^ - [E=REQ:%1]
# Subdomain 1 (folder sql-admin)
RewriteCond %{HTTP_HOST} ^sql-admin\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/phpmyadmin/.*$
RewriteRule ^(.*)$ /phpmyadmin/$1 [L]
# Subdomain 2 (folder api)
RewriteCond %{HTTP_HOST} ^api\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/api/.*$
RewriteRule ^(.*)$ /api/$1 [L]
# Subdomain 3 (folder dev)
RewriteCond %{HTTP_HOST} ^dev\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/dev/.*$
RewriteRule ^(.*)$ /dev/$1 [L]
# Avoid trailing slash from Apache
# Redirects a folder path to end in /
DirectorySlash off
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule (?>.*)(?<!/) %{ENV:REQ}/ [L,DPI,R]
But I need to enforce users connection to use https://...
Those questions are related... But doesn't all the criteria I need:
- I found this question that is related: https://stackoverflow.com/q/30863054/3286975 (I don't need redirects, also I can't modify Apache configuration from filesystem)
- But this is not what I exactly need to happen, is more like this: https://stackoverflow.com/q/10642426/3286975 (But this is missing https)
But I need for example that:
http://ex1.example.comshows the content ofhttps://www.example.com/ex1/(enforces HTTPS)https://ex1.example.comshows the content ofhttps://www.exmaple.com/ex1/(already on HTTPS)
Note: I repeat that I don't want to redirect.
I have tried to put these two lines:
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
On the main .htaccess, and on the subfolder .htaccess, but every time I reload my website an ERR_TOO_MANY_REDIRECTS error happens.
How could I achieve this?
已采纳解答
原帖链接 · MrWhite · CC BY-SA 4.0
You are missing the L flag on your HTTP to HTTPS redirect. (In order to upgrade the users connection to HTTPS you must redirect first.)
RewriteCond %{HTTPS} off
RewriteRule .? https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Without the L flag, processing is continuing through your file and getting mixed up with your internal rewrites to subdirectories.
Note that you should first test this with a 302 (temporary) redirect in order to avoid caching issues - only change it to a 301 (permanent) redirect when you are sure it's working OK.
(No need for the capturing group in the RewriteRule pattern as you aren't using a backreference in the substitution.)
Note that this simply redirects the user from HTTP to HTTPS on the same host and URL-path. Only on the second (HTTPS) request do you perform the necessary rewrites. These are two entirely separate tasks, achieved in two separate requests.
Your rewrites that follow then handle the internal rewrite from the subdomain to the subdirectory (on the same host and HTTPS). These look OK. Although they could perhaps be simplified a bit by using the REQUEST_URI server variable (as you did in your initial HTTP to HTTPS redirect) instead of using a backreference and a separate condition. For example:
# Subdomain 1 (folder sql-admin)
RewriteCond %{HTTP_HOST} ^sql-admin\.example\.com [NC]
RewriteRule !^phpmyadmin/ /phpmyadmin%{REQUEST_URI} [L]
Note that I also removed the $ from the end of the CondPattern that checks the Host header - this is to handle FQDN that contain a trailing dot (unless you are handling this elsewhere).
I'm using Cloudflare...
If you are using Cloudflare (Flexible SSL - free option) then Cloudflare acts as an SSL proxy and you will need to change your HTTP to HTTPS redirect to read:
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule .? https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Or, use a Cloudflare page rule instead - which is arguably the preferred option. Reference: https://support.cloudflare.com/hc/en-us/articles/204144518#h_a61bfdef-08dd-40f8-8888-7edd8e40d156
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
