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.
5 Responses
This was a huge help to me. Thank you!
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]
Yes this is right Ben, you can just append it to your .htaccess rule created by WordPress
You just save me a lot of time, thank a lot!
Dear Guillaume,
thank you very much for this code. I have 2 questions. Firstable if i have a domain1old and a domain2new. And wordpress installed on the server. And i want not to change the wordpress installation or the server so i want only change the domain but let the wordpress system the same but don’t want to loose seo points or broken links. Can i put this code to the .htaccess of my wordpress installation folder top of this code which is generated by wordpress :
# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ – [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
??
so that it would like this
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]
# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ – [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
i did that like this example and it worked great but i’m not sure that if that is wrong ? because there are 2 times
RewriteEngine On
RewriteBase /
thanks very much for your help.