Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.example.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.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

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

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

3 Answers 3

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} !^www.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$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 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

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

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

share|improve this answer

Your Answer

 
discard

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.