Question

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 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/mydomain_com.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/mydomain_com.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


Submit an answer

This textbox defaults to using Markdown to format your answer.

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

Sign In or Sign Up to Answer

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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

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