Webmasters Stack Exchange is a question and answer site for pro webmasters. 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'm trying to find a generic enough modrewrite rule that will acomplish following:

  • Redirect anyHost.anyTld to www.anyHost.anyTld
  • Not mess with any other subdomains, for examle other.anyHost.anyTLD should be left alone

Can you help? Does this slow down server too much?

share|improve this question

migrated from serverfault.com May 6 '11 at 23:11

This question came from our site for system and network administrators.

Zach's answer will not work with subdomains like forum.example.com, the next rules checks whether the host equals example.com. If it does, it will be redirected to www.example.com. No rules will be processed after this one (L = Last).

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

This does not cause a server slowdown, but if you really want to get every single performance bit out of it, you could create a virtual host with domain example.com with a redirect rule:

    ServerName example.com
    Redirect / http://www.example.com/
# other hosts here

Virtual Hosts documentation

share|improve this answer

You should be able to use something like this:

RewriteCond %{HTTP_HOST} !^www.(.*)$

RewriteRule (.*) $1/$2 [NC]

Let me know how it turns out, I don't have access to an Apache machine to test it right now :)

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.