3 Must-Have Htaccess Tips For You to Avoid Duplicate Content On Your Site

Optimizing a website to avoid duplicate content is a crucial issue for SEO experts

by Luigi Nori Date: 15-07-2019 htaccess apache tips


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.

<IfModule mod_rewrite.c>

  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]

</IfModule>
 
by Luigi Nori Date: 15-07-2019 htaccess apache tips hits : 646  
 
Luigi Nori

Luigi Nori

Lavora in Internet dal 1994 (praticamente una mummia), specializzato in tecnologie Web fa felici i suoi clienti smanettando con applicazioni su larga scala e ad alta disponibilità, frameworks php e js, disegno web, intercambio dati, sicurezza, e-commerce, amministrazione database e server, hacking etico. Convive felicemente con @salvietta150x40, nel (poco) tempo libero cerca di addomesticare un piccolo nano selvaggio appassionato di astri.

 
 
 

Related Posts