1

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:

But I need for example that:

  • http://ex1.example.com shows the content of https://www.example.com/ex1/ (enforces HTTPS)
  • https://ex1.example.com shows the content of https://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?

  • "http://ex1.example.com shows the content of https://www.example.com/ex1/" - but you "don't want to redirect" the user from HTTP to HTTPS?! (Although you do state that you "need to enforce users connection to use https://" - that can only be achieved with an external redirect.) – MrWhite Apr 2 at 9:32
  • Ok, okey, I didn't know, but once the .htaccess redirects one user from http to https, I need to use the same logic used there. That's the unique thing I'm missing here. Because with the current .htaccess I have shown it's working (I can access http and https, but I need to redirect http -> https as I said), but I don't know how to introduce the https redirect part without broken the entire config. – z3nth10n Apr 2 at 9:36
2

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

  • Thanks, but the error is still here. I'm using Cloudflare, should I purge cache? This is what I actually have: pastebin.com/szbpjvYr I already have checked that .htaccess has been uploaded well and removed from the subdirectories. – z3nth10n Apr 2 at 10:11
  • If you are using Cloudflare then you will need to change the method you are using to redirect, or use a Cloudflare page rule. I've updated my answer. – MrWhite Apr 2 at 10:23
  • Yes, I could see, let test it, because, with your method, the error was still present. – z3nth10n Apr 2 at 10:26
  • 1
    You will also need to make sure you have cleared your browser cache if you have been testing with 301s as these are cached persistently by the browser. (You can also test with the browser "inspector" open and "disable cache" checked on the Network tab.) – MrWhite Apr 2 at 10:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.