31

with my WordPress-Blog, I switch to https. Therefore, I have add the following code to my .htaccess-File. My Problem now is, that I get the issue "Too many Redirects". Thank you for your tips!

Domain is a Subdomain:

https://en.example.com

# Begin Force SSL
RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]
# End Force SSL
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

8 Answers 8

85

Make sure that you define the WP_SITEURL and WP_HOME at wp-config.php

define('WP_HOME','https://yourdomain.com');
define('WP_SITEURL','https://yourdomain.com');

And add this condition to check if the https at wp-config.php

    if ( isset( $_SERVER['HTTP_X_FORWARDED_PROTO'] ) && 'https' == $_SERVER['HTTP_X_FORWARDED_PROTO'] ) {
        $_SERVER['HTTPS'] = 'on';
    }
5
  • 13
    this method when use external ssl tunnel like cloadflare
    – MSS
    Commented Feb 21, 2017 at 17:06
  • 1
    After two days of crying I finally fixed it. Yes, I'm using external SSL tunnel - CloudFlare + VPS Commented Apr 28, 2020 at 22:20
  • 1
    Perfect, solved to me using a nginx reverse proxy with HTTPS. Commented Jun 14, 2021 at 0:38
  • 1
    great its working .its cause by cloudflare Commented Mar 10, 2023 at 21:41
  • That bit about setting $_SERVER['HTTPS'] = 'on'; did the trick when running on Kubernetes and Nginx Ingress
    – Kikin-Sama
    Commented Jun 1, 2023 at 17:14
76

I have soved the issue using the below code inside wp-config.php

define('WP_HOME','https://mywebsite.com');
define('WP_SITEURL','https://mywebsite.com');
$_SERVER['HTTPS'] = 'on';
7
  • 22
    With Cloudflare $_SERVER['HTTPS'] = 'on'; was enough for me.
    – Edgar
    Commented Dec 2, 2018 at 15:05
  • This, in combination with adding the rewrite rules in .htaccess worked for me too! Thanks! Commented Jan 9, 2019 at 22:17
  • This saved my life. I spent full week to fix TOO MANY REDIRECTS error but did not get success. But these small but important lines saved me. Commented Mar 8, 2019 at 6:29
  • It woked for me too. Had spent hours trying to figure this out but finally theses three lines solved my problem. Thank you so much for posting this.
    – Amit Verma
    Commented May 3, 2019 at 7:38
  • 1
    With AWS ELB terminating SSL $_SERVER['HTTPS'] = 'on'; was enough for me
    – marca
    Commented Jan 28, 2020 at 22:15
22

I had same problem nginx and had to do following in .htaccess

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]

in wp-config.php

define('FORCE_SSL_ADMIN', true);
define('RELOCATE', TRUE);
$_SERVER['HTTPS'] = 'on';
define('WP_HOME', 'https://yourdomain.com');
define('WP_SITEURL', 'https://yourdomain.com');

don't use the below with nginx unless you make sure it passes HTTPS is on to fast cgi, otherwise it won't work,

if ( isset( $_SERVER['HTTP_X_FORWARDED_PROTO'] ) && 'https' == $_SERVER['HTTP_X_FORWARDED_PROTO'] ) {
    $_SERVER['HTTPS'] = 'on';
}

it might be of benefit log debug level in error log of nginx, this is how you get to see the flow.

8
  • 1
    Just saved our day! Is there any problem using RELOCATE?
    – Tiago
    Commented Feb 22, 2019 at 16:46
  • 2
    Thanks. Only this line $_SERVER['HTTPS'] = 'on'; solves the problem for me Commented Oct 5, 2019 at 12:26
  • That saves me. Thanks a lot mate!
    – Kliptu
    Commented Nov 26, 2019 at 8:52
  • this one definitely fixed it Commented Jan 18, 2020 at 20:32
  • Thanks this fixed. I think WP_HOME shouldn't have www and ending '/'
    – Sameera K
    Commented Feb 10, 2020 at 10:19
8

Make sure that you define the WP_SITEURL and WP_HOME at wp-config.php

define('WP_HOME','https://yourdomain.com');
define('WP_SITEURL','https://yourdomain.com');

And add this condition to check if the https at wp-config.php

if ( isset( $_SERVER['HTTP_X_FORWARDED_PROTO'] ) && 'https' == $_SERVER['HTTP_X_FORWARDED_PROTO'] ) {
    $_SERVER['HTTPS'] = 'on';
}

This works well

1
  • Nice, this way i can solve the problem without changes in .htaccess, thanks!
    – girorme
    Commented Mar 24, 2023 at 16:29
8

if use Cloudflare and Nginx:

1- go to the cloud flare and add always use HTTPS in page rules.

2- add the below code into the wp-config.php

define('WP_HOME','https://example.com/blog');
define('WP_SITEURL','https://example.com/blog');

if ( isset( $_SERVER['HTTP_X_FORWARDED_PROTO'] ) && 'https' == $_SERVER['HTTP_X_FORWARDED_PROTO'] ) {
    $_SERVER['HTTPS'] = 'on';
}

Note: don't change anything in the database and using incognito mode.

1
  • 1
    Nice, this way i can solve the problem without changes in .htaccess, thanks!
    – girorme
    Commented Mar 24, 2023 at 16:29
7

Try changing your first three lines to:

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]

I believe, that unless you attempted to go to https://en.example.com/:443 then {SERVER_PORT} will never return 443.

7
  • Is there a publicly-accessible URL to view your site's behavior? Commented Apr 6, 2015 at 20:29
  • Oh there is still a problem: Currently I´m also able to open the URL with http only.
    – mm1975
    Commented Apr 7, 2015 at 13:11
  • So, it isn't forwarding you to https? Commented Apr 7, 2015 at 15:04
  • sorry for the late response. Now it works! I think, it was a server cache issue.
    – mm1975
    Commented Apr 11, 2015 at 8:20
  • 1
    What if I get the ERR_TOO_MANY_REDIRECTS error? How do I fix that?
    – Pathros
    Commented Mar 21, 2016 at 16:18
3

Excerpt: Adding the following lines of code at the start of wp-config.php file resolved the redirect conflict.

if (strpos($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') !== false)
   $_SERVER['HTTPS']='on';
0

Not directly the answer, but something else to note is that some browsers will cache redirects, so you might be changing your htaccess or wp-config without seeing any changes.

Best way to debug this is using incognito mode.

The above answers worked for me, but it took ages to work out that they had actually worked because of the browser redirect caching!

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