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 am trying to redirect a to I cannot figure out what I'm doing wrong.

I have ensured that mod_rewrite is enabled in httpd.conf with:

LoadModule rewrite_module modules/

I further verify this by running: httpd -M and getting rewrite_module (shared) included in the results.

Later in the same httpd.conf file is the VirtualHost directive where I am trying to perform the rewrite:

<VirtualHost *:80>
    Options +FollowSymlinks
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^ [NC]
    RewriteRule ^(.*)$$1 [R=301,NC,L]

    DocumentRoot /var/www/html
    ServerAlias *
        <Directory "/var/www/html">
                Options Includes FollowSymLinks
                AllowOverride All
                Order allow,deny
                Allow from all
    ErrorLog /var/www/logs/error_log
    CustomLog /var/www/logs/access_log common

As we can see, we are following SymLinks in the directory (which I believe is a requirement), AND we allow All Overrides (which meets another requirement). But obviously I'm still doing something wrong.

Can you spot it?

share|improve this question

Two issues here:

  1. You have only rewritten to, not any other hostname. I think you'll want something like:

    RewriteCond %{HTTP_HOST} ! [NC]
  2. You also need to put the rewrite rule into a <VirtualHost> which answers for the hostnames you want to redirect, that is, it contains the ServerName and/or ServerAlias entries for the hostnames to be redirected.

share|improve this answer
There is another domain on this server (although that's going to change in the next few weeks). Until that other domain is migrated, I don't think I would want to use the NOT declaration, as I feel that would break the other domain (basically, there's two different VirtualHosts on this machine). As for #2, when you say it "answers" for the hostnames I want to redirect, to you mean <VirtualHost hostname:80> instead of <VirtualHost *:80>, or do you mean I should include the .com under ServerName / ServerAlias? – David W Oct 3 '12 at 1:45
up vote 0 down vote accepted

Well, after hours working on this, I spent another 2 or 3 frustrating hours this afternoon on the project, left for a while, and just sat down to this again. Five minutes later, it was working.

Here's what I did over the course of the last few hours:

  • Split all of the vhosts off into their own /etc/httpd/conf.d/*.conf file
  • Moved the rewrite function from inside the vhost to inside the .htaccess file for the website

In the end, the successful rewrite rule (which I just tweaked and successfully tested) looks like this (inside .htaccess):

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

Key take-aways:

  • Always escape all of your periods in your Rewrite Condition
  • When you build a server, do it right the first time. It will make your life easier if you split out the vhosts into their own separate files. I've been meaning to split off each website on this server into its own vhost conf file instead of having the configs being in httpd.conf for a long time now.
  • When you want to pull out your hair, don't give up. Check your logs and keep testing things (I turned on the RewriteLog directive to help me in the troubleshooting - did you know that was a directive available within mod_rewrite? Yeah, I didn't know that either).
  • When you aren't successful in using 1 method, try another. I made constant tweaks to httpd.conf, .htaccess, AND the example.conf files.
  • Always clear your cache, and make sure you restart Apache when you make changes. I did this constantly.

I'm just glad this is over... a 5 minute job turned into a 5 hour+ job. Good grief.

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.