2

HTTPS is working and HTTP is working, but when I try to add the redirect of HTTP to HTTPS in .htaccess file, it gets caught in a redirect loop.

Here is apache:

<VirtualHost *:80>
        DocumentRoot /var/www/html/mysite

        ServerName mysite.com
        ServerAlias www.mysite.com

</VirtualHost>


<VirtualHost *:443>

        DocumentRoot /var/www/html/mysite

        ServerName mysite.com
        ServerAlias www.mysite.com

        SSLEngine On
        SSLCertificateFile /var/SSLs/somecert.crt
        SSLCertificateKeyFile /var/SSLs/somekey.key
        SSLCACertificateFile /var/SSLs/trust.crt

</VirtualHost>

Here is .htaccess file:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On

RewriteBase /

# RewriteCond %{ENV:HTTPS} !=on
# RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]


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



</IfModule>

# END WordPress

When I uncomment the RewriteCond, then it gets caught in a loop. In Wordpress I've set the site and home url to https.

I also added the following to the wp-config.php which solved https not working previously:

$_SERVER['HTTPS'] = 'on';

Appreciate any help. Note that I replaced the actual domain with mysite

coreyg
  • 428
  • 8
  • 18

1 Answers1

1
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

# I added these two lines for redirect to HTTPS
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://www.yoursite.com/$1 [R=301,L]
# (end of custom modifications)

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

you should check these helping links :

Correctly switching between HTTP and HTTPS using .htaccess

Redirect Loop while redirecting all http requests to https using .htaccess

R.K.Bhardwaj
  • 2,168
  • 1
  • 14
  • 24