Now more and more websites use SSL, which the URL start from “https”. We can set WordPress websites to use https by settings->general->WordPress Address (URL) & Site Address (URL) to “”. But one problem is when visitor put, the URL can’t redirect to “https”, so we can use .htaccess to redirect HTTP to HTTPs by followed code.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

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

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

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

The first red paragraph is for http to https and second red paragraph is for to (Tested for CentOS).

For some servers, such as Ubuntu, aboved http to https code may can’t work or have some problems, so you can use followed code:

RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

Use folloed for to

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

In fact, the wordpress will add most code to .htaccess file, so we mainly add the red part code, the http will redirect HTTP to HTTPs.


You may also like

Leave A Comment

Please enter your name. Please enter an valid email address. Please enter message.