Posts Tagged ‘Apache’

Canonical SEO with Apache Virtual Hosts Redirect

Posted in Development on February 13th, 2009 by jeremy – Be the first to comment

Since we  will be migrating domains in the coming future, today we setup a little virtual host loven in Apache.  While most folks know the importance of setting up proper canonical structures with 301 redirects and the like for SEO purposes, I figure I’ll post just in case someone doesn’t.

First a basic virtual host setup:

<VirtualHost *:80>
ServerAdmin administrator@socialguides.com
DocumentRoot /var/www/socialguides
ServerName www.socialguides.com
ErrorLog logs/error_log
CustomLog logs/access_log common
</VirtualHost>

Now that gets us going for adding another domain on the same apache server.  You can add as many of these virtualhosts blocks as you need domains supported.

Okay so next is to get a little SEO in play.  One of the most talked about things on the seo side is getting the canonical structure of your website setup properly so the www and non www dont end up being seen as two different sites to google and the like.  The downside is obvious if www.socialguides.com and socialguides.com are treated separately then your pagerank could be split between.

While it doesn’t matter which version you pick, you do need to pick one and roll with it.  Now a mistake that gets made occasionally is people just aliasing one to the other.  Something like using Apache’s ServerAlias directive to take all requests that dont match your preferred “www.socialguides.com” and show it anyways.  The problem though is while a user then doesn’t get a dreaded 404 error, you’ve only really just masked things and our search engine friends like GoogleBot will still think they are separate.  Instead you want to send with it a 301 permanent redirect in the header so the crawlers know to unify the link juice.

With that we need to add another virtual host entry with a redirectmatch rule.

<VirtualHost *:80>
ServerName socialguides.redirect
ServerAlias socialguides.com w.socialguides.com ww.socialguides.com wwww.socialguides.com
RedirectMatch 301 /?(.*) http://www.socialguides.com/$1
</VirtualHost>

The reason we add a completely new entry all together is that it fixes some annoying bugs with infinate redirect loops as well as gives you the ability to catch the “bad requests” in separate logs if so desired.  If your not using a virtualhost directive then all you really need is the redirectmatch.

Anyways as you can see we can now catch non-www traffic (as well as typos) in a SEO friendly type of way.