1

This question builds on this stackexchange-thread

The question of mentioned thread was how to write a proper http-to-https and nonwww-to-www redirect, that Google Pagespeed Insights would approve of. Stephen provided this clean answer:

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule (.*) https://www.example.com/$1 [L,R=301]

However when implementing this code it runs into "The page isn’t redirecting properly" errors for all sub pages, e.g. example.com/xyz. The code seems to miss something to cover such scenarios.

With help of Google I changed Stephen's code to the below which seems to have resolved the issue with sub pages not redirecting (note I merely replaced "(.*)" by "^" ), however I'm no expert here and in all honesty don't really know what I'm doing. Moreover it seems this new code does cause some other (to me seemingly random) redirect issues.

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^ https://www.example.com/$1 [L,R=301]

TLDR / Question: how to fix above code snippet(s), so that the https and www redirects will work for all of example.com's URLs, including all of its sub pages?

Note I had to create a separate thread because I'm lacking the reputation to respond to Stephen's answer directly..

12
  • 1
    What else do you have in your .htaccess file? When it isn't redirecting properly, where is it redirecting? Can you run curl --head https://example.com/ on the command line using curl.se for all four variants of your URL, as well as four more with sub pages and report the Location: header? – Stephen Ostermiller May 27 at 9:36
  • Hi @Stephen Ostermiller, thanks a lot for your thoughts. There is nothing else in the .htaccess file. If I implement your code again and use my browser's developer mode to see what "Network" information passes by when loading sub pages, it seems the problem is a 301 redirect loop. I see like twenty 301-lines after each other until the loading gets killed and the "The page isn’t redirecting properly" message appears. Please note that for my homepage this issue does not occur. I'm not familiar with curl.se. Can I run it on Windows too? And is it still helpful after my answers above? – Mike May 27 at 11:05
  • "the problem is a 301 redirect loop" - but what is it redirecting from/to specifically? There is nothing "wrong" with the first rule. The resulting "error" is most certainly due to configuration differences (or application code) on your server. (There is no single "correct" HTTP to HTTPS redirect that will work on all servers.) However, your updated code does not really make sense (it redirects all sub pages to the homepage)... if that code avoids the "redirect" loop then it would seem to imply you perhaps have a conflicting redirect in your application code? – MrWhite May 27 at 11:19
  • In addition to the Location: header mentioned above, it might be necessary to examine all the HTTP response headers your server is sending back. – MrWhite May 27 at 11:22
  • Aside... you should test with 302 (temporary) redirects (and/or with the browser inspector open and "caching disabled") - 301s are cached persistently by the browser, so can make testing problematic. After implementing an erroneous redirect, you will likely see a cached response, even after changing your directives. – MrWhite May 27 at 11:27
1

This should work:

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
4
  • Hi Michael, thanks for the response but that code unfortunately doesn't work for me. It results in a "The page isn’t redirecting properly" error for every page. (Of course I did replace example.com by my own domain..) – Mike May 27 at 8:15
  • 1
    Have you spoken to tech support at your host? Maybe they have something in place at the server level that overrides what you put in your .htaccess file. – Michael Moriarty May 27 at 11:19
  • 1
    Hi Michael, thanks a lot for your comment. I contacted my host's support and the problem turned out to be caused by the fact that they were already redirecting https to http at their own behalf (hence the code I added caused the loop!). On a related note; how can I vote up comments like yours that have been helpful to me but exist in a comment thread like this one? – Mike May 27 at 12:12
  • I am glad you got the problem resolved. I do not believe you can upvote comments, only answers. – Michael Moriarty May 27 at 12:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.