0

I have a problem with redirecting my website from

https://example.com to https://www.example.com

I've found a lot of advices (means rewrite rule to htaccess) on the Stack Overflow and serverfault too, but non of them works for my case.

The certificate was delivered to me for domain with www so I had to move website to such addres (cause before website was without www). The

http://example.com to https://www.example.com

works correctly.

The website is built on WordPress so I also tried to install some plugins to set up such redirection but without success.

Can anyone help me? Does somebody have an idea what can I do?

My actuall .htaccess file

# 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]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule (.*) https://www.example.com/$1 [R=301,L] 

</IfModule>

# END WordPress

Please note I am the beginner so maybe the solution is very simple and obvious but well... I'm enough familiar with this topic... Please help!

Todra
  • 1
  • 1
    Your TLS certificate should have both www. and non-www domain names included. If it did not, contact the certificate issuer and have the certificate fixed. – Michael Hampton May 23 '18 at 19:38
  • 2
    Possible duplicate of [Redirect, Change URLs or Redirect HTTP to HTTPS in Apache - Everything You Ever Wanted to Know About Mod\_Rewrite Rules but Were Afraid to Ask](https://serverfault.com/questions/214512/redirect-change-urls-or-redirect-http-to-https-in-apache-everything-you-ever) – Jenny D May 24 '18 at 06:35

1 Answers1

0

The following rewrite rule will do the trick:

#Rewrite non https to https

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{SERVER_NAME}/$1 [R,L]

#Rewrite non-www to www

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

Also, Don't forget to change your wordpress URL to www and https or it will create a loop.
To change WordPress URL edit wp-config.php and add the following lines under WP_DEBUG line:

define('WP_HOME','https://www.example.com');
define('WP_SITEURL','http://www.example.com');

As far as I am aware, if you change the wordpress URL to www, it will automatically redirect non www wordpress urls to WWW, so if you face any redirect loops, just remove the lines after

#Rewrite non-www to www

from htaccess and you will be good to go.
Hope this helps.
Shahriar

Shahriar Shojib
  • 332
  • 1
  • 2
  • 11