One of the biggest concerns for any website owner as Google’s algorithm continues to develop is most definitely duplicate content and other potentially misleading signals which could harm their website in search results. Fortunately, there is an easy way to avoid this problem with a simple .htaccess file.

301 Redirect with .htaccess

A .htaccess is a directory-level configuration file.

Let’s use this basic configuration file to ensure that Google only finds one version of your website.

This should erase any possibility of you running into duplicate content issues in future, unless of course you make the conscious decision to utilise duplicate content on your website – something I wouldn’t advise.

Redirect non-www to www

This first code will redirect the visitors who are accessing your website via non-www (or simply http://) to the www version. I’m not entirely sure why, but this version has always been my personal favourite.

Simply copy and paste the code below into your .htaccess file.

Here’s the code:

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

Redirect www to non-www

This second code will redirect the visitors who are accessing your website via www (or simply http://www.) to the non-www version. I’m not sure why you’d want to do this, but it’s only fair to provide both options.

Again, simply copy and paste the code below into your .htaccess file.

Here’s the code:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^my-domain\.com$ [NC]
RewriteRule ^(.*)$ http://my-domain.com/$1 [R=301,L]

There you go – hope that helped some of you!