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 domain.com/* addresses were 301 redirected to www.domain.com/*, 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} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]

And this works, redirecting all domain.com/* pages to www.domain.com/*, except for when I visit domain.com/forum. If I access my forum by visiting www.domain.com/forum, for some reason it's redirected to domain.com/forum (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} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/forum/$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?

link|improve this question
feedback

2 Answers

You need only one .htaccess in root:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=permanent,L]
link|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 www.domain.com/forum is again redirecting to domain.com/forum, though domain.com/<anything else> is redirecting to www.domain.com/<anything else> properly. – AmIDoingThisRight Sep 30 '10 at 15:12
feedback

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, domain.com/xyz redirected to www.domain.com/xyz.

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

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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