Search code examples
php.htaccesshttp-redirectslim-3

Proper redirection to avoid www/non-www pages being counted as duplicates


Site audits (Semrush, Seobility) point out that one of our biggest issues is duplicate content, stating that "This website uses URLs with www and non-www subdomain. This may cause duplicate content and bad links to your website."

So, basically www.oursite.com/about and oursite.com/about are counted as duplicate:

enter image description here

The site is built in PHP with the Slim framework. A middleware handles the use of https across the whole site. In addition, currently we have an .htaccess as follows:

RewriteCond %{HTTP_HOST} ^(www\.)?oursite\.com$ [NC]
RewriteRule ^(?!public)(.*)$ /public/$1 [L]

This code manages the routing of all pages through the public folder.

How can we edit this code in order to avoid www and non-www addresses being counted as duplicates?


Solution

  • Our preference would be without www.

    Assuming you're not linking to absolute URLs in your HTML source (which would need to be updated) then you could add something like the following before your existing rewrite:

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

    If the requested host starts www. then redirect to the canonical URL.

    A middleware handles the use of https across the whole site.

    Just to note that this may result in 2 redirects when requesting http://www.example.com/ (HTTP + www). But that isn't necessarily a bad thing.