Forcing a Trailing Slash With Mod_rewrite

I’m doing some SEO optimization for a couple of websites. And one of the small steps is to force a trailing slash on all URL’s that do not point to a file on the server. The reason I’m doing this is to go around the duplicate content problem.

/trip/80 /trip/80/

Both point to the same content. But a crawler would see this as two different URL’s. Which off course is correct. But this can lead to duplicate content being indexed in search engines. Which could result in penalties for the site in question. So to fix this problem we add three simple lines to the main .htaccess file

# If a requested file does not exists
RewriteCond %{REQUEST_FILENAME} !-f

# If a requested directory does not exist
RewriteCond %{REQUEST_FILENAME} !-d

# Force a trailing slash to the request
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]
comments powered by Disqus