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 have a website that's migrated from an old domain name (e.g. olddomain.com) to a new domain name (e.g. newdomain.com).

For SEO reasons, I need to rewrite all website traffic to the primary new domain name (e.g. www.newdomain.com). Unfortunately, I don't know how to add multiple OR type rewrite conditions. It seems that with all the conditions, sample code below, I get an AND condition.

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        ServerName newdomain.com
        ServerAlias www.newdomain.com
        ServerAlias olddomain.com
        ServerAlias www.olddomain.com
        DocumentRoot /var/www/newdomain.com/www/
        <Directory />
                Options FollowSymLinks
                AllowOverride All
        </Directory>

        ErrorLog /var/log/apache2/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        RewriteEngine on
        RewriteCond %{HTTP_HOST} ^olddomain.com [NC]
        RewriteCond %{HTTP_HOST} ^www.olddomain.com [NC]
        RewriteCond %{HTTP_HOST} ^newdomain.com [NC]
        RewriteRule ^/(.*)$ http://www.newdomain.com/$1 [R=301,NC]

        CustomLog /var/log/apache2/access.log combined
</VirtualHost>

Can someone give me a little assist? Any help would be greatly appreciated.

share|improve this question

2 Answers 2

up vote 3 down vote accepted

Despite the fact that you have already found working for you solution, I still will post this as yours is far from being optimal.

Solution #1: Replace [NC] by [NC,OR] in first 2 RewriteCond lines of the original code. By default one RewriteCond linked to another RewriteCond by logical AND. This will instruct Apache to use OR logic:

RewriteCond %{HTTP_HOST} ^olddomain.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.com [NC,OR]
RewriteCond %{HTTP_HOST} ^newdomain.com [NC]
RewriteRule ^/(.*)$ http://www.newdomain.com/$1 [R=301,L,NC]

Solution #2: Since you have only 4 domain names in total, it will be much easier to use opposite approach - redirect from ANY domain except correct one:

RewriteCond %{HTTP_HOST} !^www.newdomain.com [NC]
RewriteRule ^/(.*)$ http://www.newdomain.com/$1 [R=301,L,NC]
share|improve this answer
    
Solution #2 is the better one, though if you've got www.newdomain.com and test.newdomain.com or something like that, it might be better to match against !newdomain.com$ as long as the old domain didn't end in "newdomain.com" like "thisisanoldnewdomain.com". More to the point with #1, I can't find [OR] documented anywhere. Does that really work? –  DerfK Jul 22 '11 at 19:12
    
@DerfK docs -- you have to scroll quite a bit: "'ornext|OR' (or next condition) Use this to combine rule conditions with a local OR instead of the implicit AND." –  LazyOne Jul 22 '11 at 19:19
    
Hah, I never saw that. Thanks –  DerfK Jul 22 '11 at 19:28
    
Awesome! Thanks @LazyOne. –  gurun8 Jul 22 '11 at 22:22

Just found this quote:

rewriteConds apply only to the single rewriteRule that follows them.

here: http://www.webmasterworld.com/apache/3350200.htm

Here's the working solution for me:

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^olddomain.com [NC]
    RewriteRule ^/(.*)$ http://www.newdomain.com/$1 [R=301,NC]

    RewriteCond %{HTTP_HOST} ^www.olddomain.com [NC]
    RewriteRule ^/(.*)$ http://www.newdomain.com/$1 [R=301,NC]

    RewriteCond %{HTTP_HOST} ^newdomain.com [NC]
    RewriteRule ^/(.*)$ http://www.newdomain.com/$1 [R=301,NC]
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.