1

I'm trying to redirect http://sub.domain.com to https://sub.domain.com.

I have turned the web upside down trying every .htaccess and PHP script out there, and all of them give me the "Too Many Redirects" error.

I'm not so much looking for a solution, as I have pretty much given up on that.

What I'm wondering why I get the error? What does the error actually mean? As far as I can tell, all the scripts I've tried only ever redirect once.

  • 2
    Can you provide us with an example the .htaccess or PHP code you've tried? Do you have a Page Rule to use HTTPS already enabled in CloudFlare (as you commented here you were using)? Did you turn off proxying (in DNS click the orange cloud to grey) and purge the cache (under Cache) while trying your code? BTW, it's best to switch on Development Mode while trying changes to your site. – dan Jun 5 '16 at 1:35
  • 3
    As dan suggests, it sounds like you have a front-end proxy that is handling SSL and you don't actually have a security cert installed on your server. Your server (ie. in .htaccess) is unable to detect the SSL connection (since your server itself is not on SSL), hence the redirect loop. See also: webmasters.stackexchange.com/questions/93612/… – MrWhite Jun 5 '16 at 10:16
  • Are you using WordPress? It's known to need extra steps to work with CloudFlare. – Andrew Lott Jun 5 '16 at 16:34
  • @dan Thanks. Yes, I've already tried these. I was using PageRules (that worked fine), but the problem is I don't want all subdomain's redirecting, and PageRules don't have exceptions. – Josh N Jun 6 '16 at 4:01
  • 1
    @JoshN OK. It seems like the edits made confuses the original question in regards to what was causing the Too_Many_Redirects error, which I think we're clear on now. If you have another question, or extension of this one, can you start a new question (and edit that out of this one) so we can consider this one closed? – dan Jun 6 '16 at 4:44
2

the "Too many redirects" issue comes from the fact that redirects are tied together. For example, http://example.com redirects to https://example.com and https://example.com redirects back to http://example.com.

This process can repeat itself over and over again until the redirect count established by the server is reached, then when that is exceeded, the server then stops trying to process redirects.

In apache, this can be discovered by seeing the "this page can be found here" message on the screen (which is an actual redirect page) instead of the behind-the-scenes automatic redirect that the browser processes.

What I would suggest you do when testing your scripts is to make them non-cacheable to get current results every time. You should also check to ensure the user requests the HTTPS version of the site, and only perform the redirect if the request isn't HTTPS. One way to check is to validate the server port number the user connects to and if the port number is 80 then make the redirect happen for the user since port 80 is HTTP.

Another thing helpful is to turn off automatic redirects in your browser while testing your pages.

If that is not possible, use redbot.org and put your URL that is causing problems in it then you'll see what the new URL is under "Location:" in the HTTP headers. Keep following the URL until you no longer see the "Location:" value. If you can't then you have an endless redirect.

  • Additionally you can clear all redirects settings from cpanel ( hosting specific) and apply redirect- url rewrite using .htacess – Vikas Avnish Jun 5 '16 at 4:49
  • "and https://example.com redirects back to http://example.com" - FWIW, it was unlikely redirecting back to http:// in this instance. The redirect always goes to https://. But because of the use of Cloudflare's Flexible SSL service, the OP was using the wrong method to detect the protocol being used in the request from the client. – MrWhite Oct 14 '17 at 23:21
2

See the following from CloudFlare: How do I fix the infinite redirect loop error after enabling Flexible SSL with WordPress?


Some users may experience redirect loops after activating CloudFlare’s free Universal Flexible SSL option. In order to fix this issue and/or get things set please follow the steps outlined below: **

WordPress Site

** 1. Install either CloudFlare Flexible SSL WordPress plugin or WordPress HTTPS plugin. Many WordPress users are using these plugins to resolve redirect loop issues in WordPress and to make the transition to https:// easier.

Note: These plugins were not created by CloudFlare.com

  1. Install the CloudFlare WordPress plugin to make sure that the original visitor IP is being passed back at the WordPress level. Since it is likely that some of your other plugins rely on original visitor IP, installing the CloudFlare WordPress plugin will restore visitor IPs back to the WordPress level and not show CloudFlare’s IPs.

Other sites

1.Go to your website over https:// to see if your website loads. Given that you didn’t have the domain set to use SSL prior to activating Universal SSL, it is likely that you will have a mixed content error warning when you look in the corner of your browser window. In order to make this error go away, you will need to go through your site and make sure assets are being loaded over HTTPS and not HTTP. Solutions can be found at:

Mozilla’s tutorial on fixing mixed content errors

WordPress Plugin: SSL Insecure Content Fixer

  1. Create a page rule with the Always Use HTTPS action for the pattern **http://*example.com/***, replacing example.com with your domain to redirect all HTTP requests to HTTPS at our edge.

  2. If you are still seeing the error, check to make sure there are not any redirects created at your origin server. With WordPress, these will generally be created in the ".htaccess" file. If you are using the Page Rule above, remove any redirect code here to prevent any infinite loops. Otherwise, if you'd prefer to use the redirect implemented on your server rather than the Page Rule, the redirect code may need to use the "X-Forwarded-Proto" header which checks the protocol sent from the visitor. The code itself would look like this:

    RewriteCond %{HTTP:X-Forwarded-Proto} =http RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

This will have the redirect only trigger on HTTP connections, preventing the loop from occurring.

  • Thank-you Abrar. Unfortunately, I'm not using Wordpress. – Josh N Jun 6 '16 at 4:06

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.