samuelhauser
2/7/2014 - 7:48 AM

HOW TO 301 REDIRECT A TRAILING SLASH IN .HTACCESS

If you decide to redirect all pages with a trailing slash to an URL without trailing slash, the following code will do this for you:
When optimizing your website for duplicatie content, a common issue is the trailing slash. The trailing slash is the backslash at the end of an URL, like www.ralphvanderpauw.com/blog/. This URL would link to the same page as www.ralphvanderpauw.com/blog, which would create a duplicate content situation (two pages with a different link but the same content). Because search engines divide link authority over both links, you will receive less value for the page.
For this reason we implement a 301 redirection to the page with the trailing slash. This can be done with a few lines of code in the .htaccess file allowing you to redirect all incoming traffic without a trailing slash to the same URL. The code in the example below will do this automaticly for you (make sure domain.com is replaced with your root domain).
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} !^\.localhost$ [NC]
RewriteRule ^(.+[^/])/$ http://%{HTTP_HOST}/$1 [R=301,L]
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [L,R=301]