Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I just moved to a new host and they've got a different control panel than the one I was using before. On my old host, all* addresses were 301 redirected to*, without me having to configure anything and I'm trying to reproduce this on my current host.

After doing some searching, I found the following .htaccess code, which I put in my site's root .htaccess file:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^$ [NC]
RewriteRule ^(.*)$$1 [L,R=301]

And this works, redirecting all* pages to*, except for when I visit If I access my forum by visiting, for some reason it's redirected to (no www). It didn't do this before.

So then what I did was put a modified version of the above code at the top of my forum's .htaccess file (which is located at /forum), the only difference being the addition of "/forum" on the last line:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^$ [NC]
RewriteRule ^(.*)$$1 [L,R=301]

This works, but I'm just wondering if I'm going about doing this "the right way" or there's another way that's superior?

share|improve this question
RFC 2606, see section 3. – Joel Coel Feb 4 '13 at 17:49

The rules I use in the docroot's .htaccess

RewriteCond %{HTTP_HOST}   ^[^.]*?\.?([^.]+\.[^.]+)$
RewriteCond %{HTTP_HOST}   !^www\.                         [NC]
RewriteRule ^(.*)$         http://www.%1/$1                [L,R=301]
share|improve this answer

You need only one .htaccess in root:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^$ [NC]
RewriteRule ^(.*)$$1 [R=permanent,L]
share|improve this answer
So I just took out the changes I made to /forum/.htaccess, and replaced the redirect in root's .htaccess with the code you gave, and is again redirecting to, though<anything else> is redirecting to<anything else> properly. – AmIDoingThisRight Sep 30 '10 at 15:12

Do you have other rewrite rules in your .htaccess file? If so, put the first 3 lines that you showed above the other rewrite rules.

When I did this, redirected to

My .htaccess file is located in my webroot (/var/ww on Ubuntu Lucid).

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.