htaccess rewrite (old domain to new domain both www and non-www)

I am attempting to do redirect an old domain (regardless of whether it has a www prefix) to a new domain without the www. e.g.:

olddomain.com > newdomain.com

Read More

www.olddomain.com > newdomain.com

I’ve got it so that the redirects from non-www are working, but the if www is used, the domain does not redirect.

This is on a WordPress site, so I’m including the bits that WordPress uses, too.

RewriteEngine On
RewriteBase /

# ### Begin custom domain redirect ###
RewriteCond %{HTTP_HOST} ^www.olddomain.com.com$ [NC]
RewriteRule ^.*$ http://newdomain.com%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^olddomain.com$ [NC]
RewriteRule ^.*$ http://newdomain.com%{REQUEST_URI} [R=301,L]
# ### End custom domain redirect, back to the standard WP stuff ###

RewriteRule ^/index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

Related posts

Leave a Reply

1 comment

  1. Try:

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

    which will handle incoming URLs with and without the www.

    And move the RewriteBase / to above the WP block; it may be causing a problem being in the wrong place.

    RewriteBase /
    RewriteRule ^index.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]