13

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.

gurun8
  • 335
  • 1
  • 4
  • 11

2 Answers2

19

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]
LazyOne
  • 3,014
  • 1
  • 16
  • 15
  • 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](http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritecond) -- 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
  • Remember not to use space in your conditions. [NC,OR] Good [NC, OR] Bad, 500 – Grindlay Feb 14 '22 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]
gurun8
  • 335
  • 1
  • 4
  • 11
  • 2
    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 Jan 24 '20 at 15:45