Howto setup a 301 redirect

A few links to setup 301 Permanent Redirect (302 follows the same logic but is for Temporary Redirect) in Apache:

http://www.askdavetaylor.com/how_do_i_add_a_301_redirect_to_my_apache_httpdconf.html

http://www.webconfs.com/how-to-redirect-a-webpage.php

http://www.stepforth.com/resources/web-marketing-knowledgebase/non-www-redirect

One thing worth attention is the SEO-friendly redirection of non-www traffic to www. This means that if you load, say, http://example.com, you will be redirected to http://www.example.com. This avoids having two different sites registered in search engines, and allows for everybody to use the same unique URL to really use your service.

This is done through a series of little things. First off, you need to install and enable mod_rewrite. On a Debian/Ubuntu system, this would be done by

sudo a2enmod rewrite

Then you need to define both URLs in your virtual host. Something like this:

ServerName www.example.com

ServerAlias example.com

Then you need to make sure rewrite is enabled for this vhost, and define the rule to rewrite the url (taken from an example on canonical hostnames in the Apache documentation):

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.chamiluda.org [NC]
RewriteRule ^/(.*) http://www.chamiluda.org/$1 [L,R]

This should be the right syntax to do it. The rest of your virtual host config can keep working as usual.


Related Posts

SSL certificates for multiple virtual hosts: problem on IE for Windows XP

Using a single server for multiple virtual hosts is something that comes naturally...

HOWTO Authenticate access to parts of webserver using PAM and Apache

This article is incomplete and was first written in June 2006 for the...

Quick Contact Form