Use .htaccess to force https without the www

I recently updated the hosting for a single page site which was first published five years ago, to only be accessible via a SSL and within a day or two Google had boosted it from nowhere to first place for the name of the brand. There really is no question that any successful site now needs to be accessible via https only nowadays then, but even if you can set this via cpanel you should also set it via an htaccess file. Why? Because it’s really easy to forget that this may still leave you with a duplicate content problem as your site is still available both with and without a preceding www.

Personally, I like to remove the www as it means all marketing can be done using just the domain name and the solution is simple, works for most linux hosting (but not if you’re using nginx!), and even WordPress sites.

Add this code to the top of your htaccess file, having changed ‘yoursite.co.uk’ to the domain you’re affecting:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://yoursite.co.uk/$1 [R,L]
</IfModule>