Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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

There are plenty of examples here of how to redirect to, but I'm looking for a generic version, so the same htacess code would also work to redirect to and to I'm sure this exists (indeed, I have a vague memory I've done it before, somewhere), but I'm fairly unfamiliar with htaccess regex.

I need a way to match the condition HTTP_HOST does not begin with www..

share|improve this question
up vote 2 down vote accepted
#Enable RewriteEngine
RewriteEngine On

# For a way to check HTTP_HOST not beginning with www:    
RewriteCond %{HTTP_HOST} !^www [nc]

# As ! don't set backreferences we use this to match the hostname we need
RewriteCond %{HTTP_HOST} ^(.*)$ [nc]

# For the Redirect itself:
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
share|improve this answer
This is breaking on me, redirecting to http:///. Could this be because I have a three-part domain name ( to – TRiG Nov 8 '10 at 13:02
I think the backreference to %1 did not work correctly. You might try with the updated code snippet. – pacey Nov 8 '10 at 13:17
Thanks. I'll test that as soon as I can. (FTP server is down right now.) – TRiG Nov 10 '10 at 12:04

Look at this question on stackoverflow.

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.