Redirecting duplicate hostnames with .htaccess 
Author: Raoul R.TIP: Use a 301 redirect to prevent duplicate www and non-www URLs
My sitemap contained approximately 675 URLs, but an external site audit detected about 957 URLs. Canonical tags were already present, but both www and non-www versions could still be reached or discovered.
Adding a server-side 301 redirect to .htaccess forced all requests to the preferred www hostname:
RewriteEngine On RewriteCond %{HTTP_HOST} ^clubtaxi\.nl$ [NC] RewriteRule ^ https://www.clubtaxi.nl%{REQUEST_URI} [R=301,L]
After adding the redirect and starting a new crawl, the audit began working with the expected group of approximately 675 pages instead of the much larger duplicate set.
I also redirect the old .com domain to the current English website:
RewriteCond %{HTTP_HOST} ^(www\.)?clubtaxi\.com$ [NC] RewriteRule ^ https://www.clubtaxi.nl/en/ [R=301,L]
A few practical notes:
Make a backup of the existing .htaccess file before editing it.
Preserve any existing compression, caching or security directives.
Test both HTTP and HTTPS and both www and non-www versions.
Confirm that the redirect returns HTTP status 301 and does not create a loop.
Check the file again after exporting or uploading with WebSite X5, because generated server files may be replaced.
Canonical tags are useful, but a proper 301 redirect prevents visitors and crawlers from accessing duplicate hostname versions in the first place.

No comments