Hi, I have an Ubuntu 20.04 Droplet. Connected to a Domain and SSL Cert from Namecheap. Everything works fine. I’m trying to add a redirect from http://mydomain.com to https://www.mydomain.com. HTTPS and WWW. When I try my domain. I get an error saying “too many redirects.” I can’t access the page. No Apache. So I’m in a Redirect loop. Between www and non-www. My /etc/apache2/sites-enabled/000-default.conf is:

**<VirtualHost *:80>
ServerName mydomain.com
Redirect permanent / https://www.mydomain.com/
</VirtualHost>
<VirtualHost *:443>
ServerName mydomain.com
Redirect permanent / https://www.mydomain.com/
DocumentRoot /var/www/html
SSLEngine on
SSLCertificateFile /etc/ssl/mydomaincom.crt
SSLCertificateKeyFile /etc/ssl/mydomain
com.key
SSLCertificateChainFile /etc/ssl/mydomain_com.ca-bundle
</VirtualHost>

vim: syntax=apache ts=4 sw=4 sts=4 sr noet**

So, how can I stop the Redirect Loop? I also have /etc/apache2/sites-enabled/000-default.conf:30

**<VirtualHost *:443>
ServerName mydomain.com
DocumentRoot /var/www/html

SSLEngine on
SSLCertificateFile /etc/ssl/mydomaincom.crt
SSLCertificateKeyFile /etc/ssl/mydomain
com.key
SSLCertificateChainFile /etc/ssl/mydomain_com.ca-bundle
</VirtualHost>**

Does this look correct? Please take a look. And reply. With how I can stop the redirect. Loop. Thanks

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
1 answer

Hi @michaelheld4

You are doing two redirects and that’s not correct.

If you want to redirect the HTTPS www version of the site to the non-www or reverse, this could be the configuration:

<VirtualHost *:80> 
  ServerName example.com
  ServerAlias www.example.com

  Redirect permanent / https://example.com/
</VirtualHost>

<VirtualHost *:443>
  ServerName example.com
  ServerAlias www.example.com

  Protocols h2 http/1.1

  <If "%{HTTP_HOST} == 'www.example.com'">
    Redirect permanent / https://example.com/
  </If>

  # SSL Configuration

  # Other Apache Configuration

</VirtualHost>

Hope thats help,
Sergio Turpín