URL-Rewriting and SEO

Posted on Monday December 8th, 2008 by Matt

You’ve decided to use url-rewriting to have more readable and ‘user-friendly’ ones. That is probably a good thing, given the fact that search engines too prefer them this way. But careful, there are some pitfalls to avoid !

Indeed, with this type of configuration, we don’t make a good distinction between content and directory… and neither do robots! Therefore, http://www.mydomain.com/example and http://www.mydomain.com/example/ usually point to the same content, but appear as two different pages by search engines.

We then have a referencing dilution, and duplicates are coming to the party. How horrid !

We already cured a similar problem regarding multiple domain names in a previous article. The solution here will be of the same kind.

To avoid any duplicate annoyance, you’ll have to make sure the addresses without a trailing slash (as http://www.mydomain.com/example) point to the same address with a trailing slash (as http://www.mydomain.com/example/) with a 301error (page moved permanently).

To do so, you’ll proceed as in the following example, using a .htaccess file or modifying the configuration of the web server :

<IfModule mod_rewrite.c>
	RewriteEngine On
	RewriteBase /
	RewriteCond %{REQUEST_FILENAME} !-f
	RewriteCond %{REQUEST_URI} !-d
	RewriteCond %{REQUEST_URI} !(.*)/$
	RewriteRule ^(.*)$ http://www.mondomaine.com/$1/ [L,R=301]
</IfModule>
  • The first line states that we’re gonna use a rewrite rule, and the second that we’ll use the current directory as root.
  • RewriteCond %{REQUEST_FILENAME} !-f excludes files that really exist from this rule.
  • RewriteCond %{REQUEST_FILENAME} !-d does the same with directories really existing on the site.
  • RewriteCond %{REQUEST_URI} !(.*)/$ takes care of excluding URLs already ending with a slash.
  • Last, RewriteRule ^(.*)$ http://www.mydomain.com/$1/ [L,R=301] adds a trailing slash to URLs thereby selected, returning at the same time the appropriate error code.

Note that you have to place those rules before the pre-existing ones to make sure you get the wanted behaviour.

One Comment to 'URL-Rewriting and SEO'

  1. GarykPatton said :

    Hello. I think the article is really interesting. I am even interested in reading more. How soon will you update your blog?

Leave a Comment

Top of page
Clicky Web Analytics