Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WordPress https redirect loop

I'm trying to setup WordPress blog to subfolder on domain using SSL without success. If the blog .htaccess is set to redirect all HTTP to HTTPS then it results redirect loop. If .htaccess is not set to redirect all HTTP to HTTPS then blog is not showing properly as browser is blocking all HTTP requests as SSL is in use, also wp logins are failing in this case.

Details:
- Fresh WordPress installation
- Domain is using SSL
- WP installed on subfolder example.com/blog/
- HTTPS set to WP's home and site URL in database

Here is the blog folder .htaccess

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{SERVER_PORT} !^443$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>

Here is the main domain .htaccess:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\..+$
RewriteRule ^(.*)$ http://foobar.example.com/$1 [L,R=301]

Here is apache config for http requests: /etc/apache2/sites-available/000-default.conf

<VirtualHost *:80>
   ServerName example.com
   ServerAlias *.example.com
   ServerSignature Off
   RewriteEngine on
   RewriteCond %{HTTPS} !=on
   RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [NE,R,L]
</VirtualHost>

Any help and ideas what to check would be highly appreciated.

UPDATE: I have manually replaced all HTTP urls with HTTPS in the WP database, the only occurrences were sample page and post urls, but this didn't help to resolve the problem.

like image 313
Laowai Avatar asked Feb 09 '23 12:02

Laowai


1 Answers

I was able to resolve the problem by adding $_SERVER['HTTPS']='on'; into wp-config.php. I don't know why $_SERVER['HTTPS'] is not set properly by the system, but I guess its somehow related to Apache/SSL configurations.

Without setting $_SERVER['HTTPS']='on' WP was not able to detect HTTPS and was loading content over HTTP which with .htaccess redirects caused redirect loop.

like image 193
Laowai Avatar answered Feb 12 '23 03:02

Laowai