SEO without the snake oil
Pick one. www.example.com or example.com. It does not matter which, but you must commit, because Google, browsers, and your own analytics will treat the two as separate sites until you tell them otherwise.
The fix lives in your .htaccess file, which sits in the public root of an Apache site. Below are the rules we put on every site we ship.
Force everything to www (and HTTPS)
RewriteEngine On
# 1. Upgrade to HTTPS
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# 2. Force www
RewriteCond %{HTTP_HOST} ^example\.co\.uk$ [NC]
RewriteRule ^ https://www.example.co.uk%{REQUEST_URI} [L,R=301]
Force everything to non-www (and HTTPS)
RewriteEngine On
# 1. Upgrade to HTTPS
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# 2. Strip www
RewriteCond %{HTTP_HOST} ^www\.example\.co\.uk$ [NC]
RewriteRule ^ https://example.co.uk%{REQUEST_URI} [L,R=301]
Why the order matters
The HTTPS upgrade comes first because if a visitor arrives at http://example.co.uk we want a single 301 to take them to https://www.example.co.uk, not two hops. Browsers cache redirects aggressively; an extra hop is a measurable speed regression and Google notices.
Why R=301 not R=302
301 means permanent. It tells search engines and browsers to update their index. 302 means temporary — Google will keep both URLs in the index forever and you will keep splitting your authority between them.
What about Cloudflare or a CDN?
If your site sits behind Cloudflare, you can do this in their dashboard instead — Rules → Redirect Rules. The .htaccess version still works as a belt-and-braces, and is what kicks in if Cloudflare ever fails open.
How to verify it worked
From a terminal:
curl -I http://example.co.uk
curl -I http://www.example.co.uk
curl -I https://example.co.uk
Each one should return a single HTTP/1.1 301 Moved Permanently with a Location header pointing at https://www.example.co.uk/ (or whichever you chose). Only the canonical URL should return 200 OK.
One canonical, one set of rankings, one place your customers' bookmarks all point at. If your site is doing something stranger, we are happy to look at it.