301 redirect with htaccess for new domain in Wordpress

301 redirect with htaccess for new domain in Wordpress

I have been struggling lately for a really simple task: change a domain name for an already online site and do a 301 redirect for all pages to the new domain. Basically the domain is pointing to the same directory, I added it as an aliases to point to the old domain. After doing all the changes to the site file and Wordpress Database, I simply needed to redirect all traffic to the new domain name.

Seems easy but Htaccess can be complicated and sensitive when you are not a programmer like me.

After searching, someone came back to me with the solution, just put this before the Wordpress rewrite rule in htaccess (right at the top of the file):

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$ [NC]
RewriteRule ^.*$ http://www.newdomain.com%{REQUEST_URI} [R=301,L]

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

Basically any hit on www and non-www for the olddomain.com will be redirected to newdomain.com passing the requested URL, so no SEO headache after this.

I hope this will help someone.

3 Responses

04.14.10

This was a huge help to me. Thank you!

04.14.10

Thanks Guillaume. Just to clarify for new users, with the new version of wordpress this is how your .htaccess file should look like (example for .co.uk domain):

# BEGIN WordPress

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.olddomain\.co\.uk$ [NC]
RewriteRule ^.*$ http://www.newdomain.co.uk%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTP_HOST} ^olddomain\.co\.uk$ [NC]
RewriteRule ^.*$ http://www.newdomain.co.uk%{REQUEST_URI} [R=301,L]
RewriteRule ^index\.php$ – [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

04.14.10

Yes this is right Ben, you can just append it to your .htaccess rule created by Wordpress

Leave Your Response

* Name, Email, Comment are Required