29

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
mm1975
  • 1,583
  • 4
  • 30
  • 51

8 Answers8

80

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';
    }
Dave
  • 498
  • 2
  • 7
  • 22
abdallah Nofal
  • 1,077
  • 8
  • 13
65

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';
Ahmed
  • 1,666
  • 17
  • 23
19

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.

Nik
  • 2,885
  • 2
  • 25
  • 25
Mohammad Aboali
  • 191
  • 1
  • 2
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

Nik
  • 2,885
  • 2
  • 25
  • 25
Sudharshan R
  • 106
  • 1
  • 4
6

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.

eat-sleep-code
  • 4,753
  • 13
  • 52
  • 98
5

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.

Pirooz Jenabi
  • 440
  • 5
  • 7
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';
Chandra Kumar
  • 4,127
  • 1
  • 17
  • 25
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!

Matt Wills
  • 676
  • 6
  • 11