Fix „Error too many redirects“ in DDEV (Apache / htaccess)

Some web projects have a rewrite rule from HTTP to HTTPs in their .htaccess (for Apache):

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{HTTPS} !=on
  RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

If you try to open this in DDEV, it will result in „Error too many redirects“ because %{HTTPS} will always return „off“ in DDEV containers (not sure exactly why, but something technical with reverse proxy behavior).

There is a simple fix which should be applicable on live sites as well. Just add this additional rule for %{HTTP:X-Forwarded-Proto} to your project. It will check if HTTPS is activated via a reverse proxy:

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{HTTPS} !=on
  RewriteCond %{HTTP:X-Forwarded-Proto} !https [NC]
  RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

The resulting rewrite rules will only try a redirect if

  • %{HTTPS} is not set to „on“
  • AND
  • %{HTTP:X-Forwarded-Proto} is not „https“

Since DDEV sets X-Forwarded-Proto to „https“, the error should disappear and your live site should work as expected as well.


Big Thanks to @nurtext:

See apache-site.conf for more information. If you have a better approach, please let me know in the comments. Thanks!


2 Antworten zu „Fix „Error too many redirects“ in DDEV (Apache / htaccess)“

  1. Johannes Reiter

    Hi there,
    in the .htaccess fix is a small typo on the closing IfModule Tag.

    1. Matthias Andrasch

      Hey, oh, thanks very much! Fixed. Regards, Matthias

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert