Redirecting non-www to www https

Hi,

I am trying to redirect a non-www version of my website to https://www.website.com

The website is a Wordpress website and is currently set to https://website.com in my WP-admin

If I type http://website.com, it redirects to https://website.com, but I would like all versions including and excluding www. to go to https://www.website.com

I’ve tried the following but not having any luck.

RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) https://www.website.co.uk%{REQUEST_URI} [L,R=301]

Can anyone help me with the redirect?

Thanks

1 Like

Hi there toolman,

my htaccess file with your url would look like this…

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.website.co.uk$ [NC]
RewriteRule ^(.*)$ https://www.website.co.uk/$1 [R=301,L]

RewriteCond %{HTTPS} !^on$
RewriteRule (.*) https://www.website.co.uk/$1 [R,L]

Perhaps that will also work for you. :winky:

coothead

1 Like

Hi,

Many thanks. I’ve tried that and I’m getting this error:

"The page isn’t redirecting properly

An error occurred during a connection to www.mywebsite.co.uk.

This problem can sometimes be caused by disabling or refusing to accept cookies."

Do you have any suggestions to why this is? Could it be because in my WordPress admin I have the site URL as http://mywebsite.com?

Hi there toolman,

I do not know what problems Wordpress could cause. :unhappy:

What I can say, though, is that when I tried this link…

http://website.com

…it took me smoothly to…

https://www.website.com/

Is it possible that you are experiencing a cache problem ?

coothead

2 Likes

Thanks for the reply.

Ok, I’ve cleared my cache and I can see it forwarding to https://www.mywebsite.co.uk/

I guess it must be a setting in WordPress. I will see if I can see anything in there.

Thanks for your help!

Hi there toolman,

as a matter of interest, I use these three add-ons/extensions…

"uBlock Origin " , "Ghostery " and "I don’t care about cookies "

…without any problems. :winky:

Also note that…
.
website.com

…will work OK.

coothead

1 Like

Thanks for the tips, I will check them out :slight_smile:

If you want peeps to go to the www version then you need to change to that in WP admin.

Thanks for the reply.

I’ve changed the site URL so use www.

Do I need to change the WordPress URL in there too? I think on another site when I changed that part, it broke the website.

Thanks

Hi, you should add the below code:

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

Also, you should change the Site URL, WordPress Address URL to www version to get it work smoothly. I use to add this code on all my sites and to the site that I create newly.

1 Like

Thanks everyone for the replies.

This is what I used in the end:

RewriteEngine On 
RewriteCond %{HTTP_HOST} ^website\.co.uk [NC]
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.website.co.uk/$1 [R,L]

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]