Tag Archives: URL Trailing Slashes

URL Trailing Slashes and Canonical URLs

I noticed recently in my Google Webmaster Tools account that Google seems to be honing in on house cleaning for webmasters. Maybe it’s the recent Google updates that have come down the pike in the past twelve months and maybe it’s just my imagination but nonetheless it’s a good habit for webmasters to practice.

The one thing that lead my suspicion of this was the notes on a few of my sites that Google has begun to put into “HTML Improvements area is alerts over trailing slashes in the URL string. For reference, the following URLs are not the same to a search engine:

  1. http://www.mysite.com/page-name
  2. http://www.mysite.com/page-name/
  3. http://www.mysite.com/page-name/index.html

Because I generally don’t link to any index.html files directly inside of directories, the first two in the list were of particular concern to me and not the third.

The solution is to put the following three lines into your .htaccess file:

RewriteCond %{REQUEST_URI} !\.[^./]+$
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://www.mysite.com/$1/ [R=301,L]

That will automatically enter the trailing slash to the url if it is not already present. Remember to keep those three lines together in the .htaccess file.