How to stop "www" "non-www" Loop? Ubuntu Droplet...

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 to 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 Redirect permanent / </VirtualHost> <VirtualHost *:443> ServerName Redirect permanent / DocumentRoot /var/www/html SSLEngine on SSLCertificateFile /etc/ssl/mydomain_com.crt SSLCertificateKeyFile /etc/ssl/mydomain_com.key SSLCertificateChainFile /etc/ssl/ </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 DocumentRoot /var/www/html

SSLEngine on SSLCertificateFile /etc/ssl/mydomain_com.crt SSLCertificateKeyFile /etc/ssl/mydomain_com.key SSLCertificateChainFile /etc/ssl/ </VirtualHost>**

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


Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

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.

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> 

  Redirect permanent /

<VirtualHost *:443>

  Protocols h2 http/1.1

  <If "%{HTTP_HOST} == ''">
    Redirect permanent /

  # SSL Configuration

  # Other Apache Configuration


Hope thats help, Sergio Turpín