- by Judd Lyon
- Oct 15, 2008
- Filed in: Search engine marketing
Avoid having example.com/blah and example.com/blah/ looking like separate (and thus duplicate) pages in search engine indexes by automatically adding a trailing slash using Apache's mod_rewrite.
Here's a quick fix to a common problem known as the "Apache trailing slash problem." Trailing slashes are the proper way to reference files in Apache, yet folks (naturally) tend to leave them off when typing or hyperlinking. Many times people attempt to remedy the situation by allowing both example.com/foo and example.com/foo/ to work. The problem is that Google and other search engines see these as two different pages, yet the content is the same. Duplicate content is a major SEO no-no and should be avoided.
The fix is to rewrite URL's lacking the trailing slash to automagically add them. That way people can type either, Apache has the explicit path it needs, and Google's happy. Sounds good to me. Here's the directive to add to your .htaccess file:
# trailing slash fix
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ http://example.com/$1/ [R=301,L]
References
- Don't forget your trailing slash
- .htaccess Add Trailing Slash to URL
- .htaccess rewrite rule to add a trailing slash to requested urls
- Apache's URL Rewriting Guide
Comments
There are no comments for this entry.
