redirect entire domain with apache config

Posted: January 11, 2011 in Uncategorized

This is a mixed issue because it can be used for SEO as well as just normal redirects. The SEO part is easier to explain, so I’ll go that route. ;)

Image you have this new site you’re running: domain.com. You’re very exited to put it online and finally your customer gives his go! Lots of new and overhauled content to be put on the web with little more than a snap of your finger. So you use your mighty finger and boom, there it is: online and for everyone to see. In order to make it for everyone to find, there’s a bit more magic involved unfortunately. The magic is called search engine optimization (short SEO) and I’m not going to discuss this here in detail. But one simple rule is, that you should decide on and point out one main domain. In your case, this should be www.domain.com. To make Google and the other search engines only index the main site, you have to redirect all the requests for domain.com to www.domain.com. So far, so good, but that goes for all requests like domain.com/category1/page2.html.

To achieve this, there is a simple way in the Apache 2 Webserver, which is called the Rewrite Engine. To configure it, you have to either edit the Apache-config files (normally unter /etc/apache2/sites-available) or the .htaccess file in the root-directory of your Website. (One post earlier I discussed the custom apache-config in vhost.conf files for Plesk. You can put it there too.)

Here’s what to put there:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=permanent,L]

In my setup, I had to change the last line to (notice the missing slash before $1):

RewriteRule ^(.*)$ http://www.domain.com$1 [R=permanent,L]

This was to avoid an occuring double-slash after the domain name as in www.domain.com//page.html. I guess you’ll have to try that out yourself for your server and website.

Anyway, thanks to the guys at nixCraft, where I found the solution.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s