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 am trying to redirect a www.example.com to www.example.org. 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/mod_rewrite.so

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

    ServerAdmin admin@my-domain.com
    DocumentRoot /var/www/html
    ServerName example.org
    ServerAlias www.example.org *.example.org
        <Directory "/var/www/html">
                Options Includes FollowSymLinks
                AllowOverride All
                Order allow,deny
                Allow from all
        </Directory>
    ErrorLog /var/www/logs/error_log
    CustomLog /var/www/logs/access_log common
</VirtualHost>

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
add comment

2 Answers

Two issues here:

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

    RewriteCond %{HTTP_HOST} !www.example.org [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
add comment
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 ^(.*)$ http://www.example.org/$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
add comment

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.