14

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.

2 Answers 2

21

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]
3
  • 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
    Commented Jul 22, 2011 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
    Commented Jul 22, 2011 at 19:19
  • Remember not to use space in your conditions. [NC,OR] Good [NC, OR] Bad, 500
    – Grindlay
    Commented Feb 14, 2022 at 13:58
1

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]
1
  • 3
    As a note to anyone else visiting here years later, the solution proposed by LazyOne above (and accepted by OP) is far more optimized, especially if you have more than just two or three conditions to OR together.
    – Doktor J
    Commented Jan 24, 2020 at 15:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .