If you have multiple domains pointing at your site it is important that the site has a preferential domain so that Google does not mistakenly regard the different domains as different sites.
Most of this can be achieved within the WordPress CMS by simply setting the preferred domain but we may still need to action a generic redirect outside of WordPress itself to ensure all domain variations are correctly redirected to the preferred domain.
Root 301 Domain Redirections in WordPress .htaccess
To redirect WordPress to one version of a domain we have to add some additional code to the .htaccess as below.
Default .htaccess
The default file is going to look something like this:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ – [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
WordPress .htaccess with Domain Wide 301 Rewrite
Lets assume we have two domains pointing at our site:
www.domain-A.com
www.domain-B.com
Our site is set up in the WordPress backend as www.domain-A.com so whilst any inner pages will link to this address pages can still be linked to and still render on the www.domain-B.com and domain-B.com variations of the second URL.
To remedy this we add two new rules to our htaccess file.
- Rewrite domain-B.com to www.domain-A.com
- Rewrite www.domain-B.com to www.domain-A.com
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On RewriteBase /
# New code to redirect from domain-A.com to www.domain-B.com & include the query string
RewriteCond %{HTTP_HOST} ^domain-A.com
RewriteRule (.*) http://www.domain-B.com/$1 [R=permanent,QSA,L]
# New code to redirect from www.domain-A.com to www.domain-B.co.uk & include the query string
RewriteCond %{HTTP_HOST} ^www.domain-A.com
RewriteRule (.*) http://www.domain-B.com/$1 [R=permanent,QSA,L]
RewriteRule ^index.php$ – [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Putting it into Practice
Putting this into practice is pretty simple and you can take the example above and replace the domain-A and domain-B values and insert your own domains. This obviously only covers a site with two URLs but you can just as easily add another set of these rules with domain-C pointing to domain-A for as many URLs as are required.
Well folks, I hope that helps and if you need any help with WordPress or SEO for your WordPress site give me a shout. Oh, and please share this with the social buttons below if it helped you out! 🙂
6 Responses
Thank you for this article.
Please can you clarify the sentence ‘Lets assume we have two domains pointing at our site’, specifically could this solution work for http://www.domain.co.uk and http://www.domain.com instead of http://www.domain-A.com
and http://www.domain-B.com ?
Yep, totally, you still need to set one primary domain and WP will not rewrite by default at the site root if the hosting is set up to accept requests on both domains. Always check after making changes with web bug or some other header checker so you can make sure you are getting a 200 on your primary and a 301 on your alternative. Shout if that’s not clear.
What about subfolders which have their own .httaccess. Do we need to setup them manually like above? And do this technique have any effects to the wordpress subfolders, like wp-content, wp-include, etc? Because after implemented 301 redirect I got many visitor redirected to 501.shtml.
Hey Hafizh
Each folder can have it’s own .htaccess but it is not necessary. This will simply rewrite the domain portion to ensure your site only has one authoritative domain rather than several.
Hope that helps!
Marcus
Hi there.
Nice article! The second article almost covers what I need, so I was wondering if you can tell me what to do, if I had a sige on domain.eu and now finally got my hands on .com? When I got the .com site, I decided to do a complete rework of the site, which has resulted in some urls on the .eu domain not being equal to the new ones – but with the same content.
My question is, when I launch the .com site, how do make sure that the old .eu/about is mapped with 301 redirect to my new .com/who-am-i page and so forth? I am trying to make sure, that when the .eu site server is closed down, that whatever links to my site that Google have indexed, are 301 redirected to the ones on my new site, till Google index the new site and realise that .eu is no longer live.
Hey Nikolaj
A simple way to do this under WordPress is as follows:
1. Get a list of all URLs on the old site (screaming frog is your friend here)
2. Launch the new site
3. Point the old domain to the new site
4. Crawl the list of old URLs
5. Create redirections for the 404’s
If you use the redirection plugin it logs 404’s and simplifies step 5.
Hope that helps!
Marcus