views:

169

answers:

2

I am trying to configure my Apache 2.2 version to use a 301 permanent redirect when someone types my url without the www. I want to configure this in the httpd.conf and not using .htaccess if possible. I have tried using Redirect permanent but the first variable has to be a directory and not a url. Any ideas how to configure boom.com requests to be redirected to www.boom.com using a 301 redirect in Apache? Thanks

A: 

Add the following:

# Canonical hostnames
RewriteEngine On
RewriteCond %{HTTP_HOST}   !^www\.boom\.com [NC]
RewriteCond %{HTTP_HOST}   !=""
RewriteRule ^/(.*)         http://www.boom.com/$1 [L,R=301]

This will redirect all request which don't match www.boom.com to www.boom.com, with the same query path. (For example, boom.com/foo?foo=bar will be redirect to www.boom.com/foo?foo=bar).

tux21b
We already have a whole set of RewriteCond lines. I would prefer to not mess with these. Is this the only option?
roacha
You could put the extra RewriteCond entries in a VirtualHost block to isolate them from the rest?
Alex Poole
A: 

If you have named virtual hosts you could put the extra RewriteCond entries @tux21b gave inside to isolate them. Also if you have mod_alias you could try this which should do the same thing:

<VirtualHost boom.com:80>
    RedirectMatch permanent /.* http://www.boom.com$0
</VirtualHost>

I'm sure someone will comment if there's a reason to use one over the other.

Alex Poole