Optimizing a website to avoid duplicate content is a crucial issue for SEO experts
In order to be able to implement these tips it is necessary that your Apache server already has the mod_rewrite module activated. mod_rewrite and .htaccess are used together so that your site has the possibility to present semantic and public friendly URLs.
In the past it was more common to find sites that have addresses like www.mysite.com/index.php?id=1
. Nowadays, this is already completely out of use. It is much more interesting to use an address like this: www.mysite.com/my-good-post, do you agree?
Well, considering that you have already made the necessary settings for the .htaccess file to work properly, I present my tips used in my day-to-day.
1. Remove slash (/) at the end of the URL
An address www.mysite.com and www.mysite.com/ are different and if they have the same content, it is duplicate content. So, to avoid this, I use the following code to always remove the bar at the end of the address.RewriteCond %{HTTP_HOST} !^. [NC] RewriteRule ^(.+[^/])/$ http://%{HTTP_HOST}/$1 [R=301,L]
2. Insert the www if you don't have one.
An address www.mysite.com and mysite.com are also different addresses and is considered as duplicate content if they display the same content. So, to avoid this I use the following code:RewriteCond %{HTTP_HOST} !^www. RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
3. Remove index.php, if it exists
If you access a website with www.mysite.com and www.mysite.com/index.php and both display the same content, it will also be considered as duplicate content. In this case, the problem is solved as follows:RewriteCond %{THE_REQUEST} ^.*/index.php RewriteRule ^(.*)$ / [R=301,L]
Okay, okay, okay, okay, okay. This way you guarantee that your project will always be accessed without bar at the end (/), always with www and avoid that the home is accessed via /index.php.
Here, follows the complete code of my .htaccess used in most of my projects.
RewriteEngine on #redirect if exist end slash RewriteCond %{HTTP_HOST} !^. [NC] RewriteRule ^(.+[^/])/$ http://%{HTTP_HOST}/$1 [R=301,L] #redirect if does not exist www RewriteCond %{HTTP_HOST} !^www. RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] #redirect if exist index.php RewriteCond %{THE_REQUEST} ^.*/index.php RewriteRule ^(.*)$ / [R=301,L]