Home > Blockchain >  How do I stop a Redirect Loop? Apache2 on Ubuntu
How do I stop a Redirect Loop? Apache2 on Ubuntu

Time:10-07

I have a DigitalOcean 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://example.com to https://www.example.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 example.com
      Redirect permanent / https://www.example.com/
 </VirtualHost>
 <VirtualHost *:443>
      ServerName example.com
      Redirect permanent / https://www.example.com/
      DocumentRoot /var/www/html
      SSLEngine on
      SSLCertificateFile /etc/ssl/example_com.crt
      SSLCertificateKeyFile /etc/ssl/example_com.key
      SSLCertificateChainFile /etc/ssl/example_com.ca-bundle
 </VirtualHost>`

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

 `<VirtualHost *:443>
       ServerName example.com
       DocumentRoot /var/www/html
       SSLEngine on
       SSLCertificateFile /etc/ssl/example_com.crt
       SSLCertificateKeyFile /etc/ssl/example_com.key
       SSLCertificateChainFile /etc/ssl/example_com.ca-bundle
  </VirtualHost>`

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

CodePudding user response:

Adjust your Vhost configuration:

 <VirtualHost *:443>
      ServerName example.com
      # Redirect permanent / https://www.example.com/ <- *Remove or comment this line*
      DocumentRoot /var/www/html
      SSLEngine on
      SSLCertificateFile /etc/ssl/example_com.crt
      SSLCertificateKeyFile /etc/ssl/example_com.key
      SSLCertificateChainFile /etc/ssl/example_com.ca-bundle
 </VirtualHost>`

Then reload the Apache service, after this your redirects would be:

http://example.com -> https://www.example.com
http://www.example.com -> https://www.example.com
https://example.com -> Stays the same
https://www.example.com -> Stays the same
  • Related