When Pointing From http//www To https//www Appear Redirect Loop

July 2, 2019 176 views
Apache

Hello everyone.

I have some strange issue related to the redirect loop appearing.

For example, my website is https://www.test.org/ in the website general setting I have the same address:https://www.test.org/ in digitalocean domain setting I add that:

test.org is posting to IP
wwww.test.org is posting to the same IP

also in this file 000-default.conf (folder site enabled) I have those lines:

RewriteEngine on
RewriteCond %{SERVERNAME} =www.test.org [OR]
RewriteCond %{SERVER
NAME} =test.org
RewriteRule ^ https://%{SERVERNAME}%{REQUESTURI} [END,NE,R=permanent]

and in the .httaces file, I have nothing.

*But when I enter to the browser this one http://www.test.org/ I have those redirect chains: *

http://www.test.org/ 301 redirect to https://test.org/ 301 redirect to https://www.test.org/

Somebody could help how to avoid the second unnecessary chain element in order to get the redirect from http://www.test.org/ directly here: https://www.test.org/ and from http://test.org/ also directly here https://www.test.org/

Many thanks

2 Answers
bobbyiliev MOD July 3, 2019
Accepted Answer

Hello,

I believe that this might be an issue with your web application itself. You need to make sure that your application URL is setup correctly.

For example if you are using Wordpress you need to make sure that you update your Site and Home URLs as https://www.test.com

Or if you are using Laravel for example make sure to set the correct app URL in your .env file.

Let me know how it goes!
Bobby

  • Bobby, hi.

    As I wrote before in WordPress general settings the website is poiting as https://www.test.org/

    I suppose that the problem is in this directives that lies in this file 000-default.conf (folder site enabled):

    RewriteEngine on
    RewriteCond %{SERVERNAME} =www.test.org [OR]
    RewriteCond %{SERVERNAME} =test.org
    RewriteRule ^ https://%{SERVERNAME}%{REQUESTURI} [END,NE,R=permanent]
    

    Also I think that the problem could be in DigitalOcean Domain settings, is it right that I established? or how to make the test.org alias of wwww.test.org ?:

    A Records:

    test.org is posting to IP
    wwww.test.org is posting to the same IP

    If someone could help solve the problem I will really appreciate this.

    Many thanks

    • Thanks for the clarification! I see the problem now!

      So in your site settings you have https://www.test.com but in your Apache conf it’s redirecting to https://test.com

      You should change this line from:

      RewriteRule ^ https://%{SERVERNAME}%{REQUESTURI} [END,NE,R=permanent]
      

      To:

      RewriteRule ^ https://www.%1%{SERVERNAME}%{REQUESTURI} [END,NE,R=permanent]
      

      Make sure to run a config test before restarting Apache!

      Regards,
      Bobby

      • Bobby hi,

        Seems that it works well.

        I appreciate your help.

        Many thanks

        • Hi Jeremy,

          That’s great! I’m happy to hear that I was able to help :)

          Regards,
          Bobby

          • Bobby, hi.

            Yesterday I saw that left small issue - from this url https://test.org/ don`t work the redirect here: https://www.test.org/

            I use your code:

            RewriteEngine on
            RewriteCond %{SERVERNAME} =www.test.org [OR]
            RewriteCond %{SERVERNAME} =test.org
            RewriteRule ^ https://www.%1%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
            

            instead my old one:

            RewriteEngine on
            RewriteCond %{SERVERNAME} =www.test.org [OR]
            RewriteCond %{SERVERNAME} =test.org
            RewriteRule ^ https://%{SERVERNAME}%{REQUESTURI} [END,NE,R=permanent]
            

            So i checked all redirect scenario:

            http://www.test.org/ - redirect here https://www.test.org/
            http://test.org/ - redirect here https://www.test.org/
            https://test.org/ - don`t redirect here https://www.test.org/
            https://www.test.org/ - is ok

            So as you see this url: https://test.org/ don`t redirect here: https://www.test.org/

            Also actually I don`t know related it to this code or not, but also I use the special redirection plugin called “Redirection” for WordPress - I set-upped 301 redirect from the / - main page to another website.

            And interesting thing - when I delete this redirect via redirection plugin all seems that works well and all redirection scenaria works well, but when I use the redirection plugin only this one scenario:

            https://test.org/ - don`t redirect here https://www.test.org/

            don`t work.

            Tell, please should we customize the code that you provided or the problem in this “Redirection” plugin and could you help with this issue?

            Many thanks.

Yesterday I saw that left small issue - from this url https://test.org/ don`t work the redirect here: https://www.test.org/

I use your code:

RewriteEngine on
RewriteCond %{SERVERNAME} =www.test.org [OR]
RewriteCond %{SERVERNAME} =test.org
RewriteRule ^ https://www.%1%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

instead my old one:

RewriteEngine on
RewriteCond %{SERVERNAME} =www.test.org [OR]
RewriteCond %{SERVERNAME} =test.org
RewriteRule ^ https://%{SERVERNAME}%{REQUESTURI} [END,NE,R=permanent]

So i checked all redirect scenario:

http://www.test.org/ - redirect here https://www.test.org/
http://test.org/ - redirect here https://www.test.org/
https://test.org/ - don`t redirect here https://www.test.org/
https://www.test.org/ - is ok

So as you see this url: https://test.org/ don`t redirect here: https://www.test.org/

Also actually I don`t know related it to this code or not, but also I use the special redirection plugin called “Redirection” for WordPress - I set-upped 301 redirect from the / - main page to another website.

And interesting thing - when I delete this redirect via redirection plugin all seems that works well and all redirection scenaria works well, but when I use the redirection plugin only this one scenario:

https://test.org/ - don`t redirect here https://www.test.org/

don`t work.

Tell, please should we customize the code that you provided or the problem in this “Redirection” plugin and could you help with this issue?

Many thanks.

Have another answer? Share your knowledge.