Eric Mitjans
Eric Mitjans

Reputation: 2179

Redirecting wordpress to HTTPS

I have a .htaccess file generated by a plugin. It looks like this:

# BEGIN Far Future Expiration Plugin
<IfModule mod_expires.c>
ExpiresActive on
<FilesMatch "\.(gif|jpeg|jpg|png)$">
ExpiresDefault "access plus 168 hours"
</FilesMatch>
</IfModule>
# END Far Future Expiration Plugin

# 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

I need to implement a HTTPS redirection. I've tried adding the following to the file but it gives me a Too many redirects error.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^yoursite.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.yoursite.com [NC]
RewriteRule ^(.*)$ https://www.yoursite.com/$1 [L,R=301,NC]

Any tips?

Upvotes: 0

Views: 25

Answers (1)

cmorrissey
cmorrissey

Reputation: 8593

This code will redirect any non https request to the same page/domain with https.

RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Note: some but not all ssl certificates don't include the www or non www domain depending on how you purchased it. Due to this you may get an error and in addition to the code above you can use what you were attempting above.

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

This example will redirect non www sites to www, switch it around if you need the other case.

Upvotes: 1

Related Questions