Themed Multiple Domains in Wordpress

16th of May, 2008

Traditionally, wordpress does not allow you to use multiple domains on your site.  It will always redirect them to the URL you have specified in your options.  Generally speaking, that's exactly the right way to go about it unless you have an advanced purpose in mind.

Why would you want this?

Well, if you have multiple domains relating to your site (including subdomains) you can use this method to serve alternative content on those domains, from a single wordpress installation.  You can even use an entirely different theme for each domain, if you like.

Why did ∗I∗ want this?

Well, I am working on a project which needs to be accessible from a mobile phone or PDA.  A method using browser-detection is unreliable, simply switching out style sheets may result in elements and images being downloaded even if they're not displayed -- so I decided to create an entirely new (much lighter) wordpress theme and serve it from a .mobi domain.  After all, that's what .mobi domains are for, right?

Surely this is 'out there' already...

Well, yes, sort of.  When poking around for a ready-made solution I found a guy who thanked another guy for making a plugin to do this.  It's kind of old and not particularly well supported, so I wasn't really overjoyed at the thought of making it an essential part of my new project's future.

The other thing that bothered me is that the plugin was really quite active.  It filters all the generated links on your site, and sifts through all your content looking for absolute links to edit to match your additional domain.  This is probably exactly what you want if you have heaps of posts with hard links in them by the time you decide you want a new domain added, but I don't need that.

If that is what you want, there's a more up-to-date and quite nice-looking plugin available from this guy.

Why I didn't want a plugin

It's not that I don't like plugins, it's just that this is going to be core functionality for my new project, so I really don't want to have to wait for the plugin to be updated if there are major changes to the wordpress platform (quite common).  I want something simple, and I'm prepared to use relative links in my posts from day one.

How to do it

It's actually surprisingly simple.  You just need to modify your wp-config.php to set a few constants.

In wp-config.php find:

$table_prefix = 'wp_';

Underneath that, insert the new code:

function domain_theme($domain) {
   // edit this path to match your server
   $themepath = '/path/to/public_html/wp-content/themes/';
   define('WP_SITEURL', 'http://'.$domain);
   define('WP_HOME', 'http://'.$domain);
   define('TEMPLATEPATH', $themepath.$domain);
   define('STYLESHEETPATH', $themepath.$domain);
}
$domain = $_SERVER['SERVER_NAME'];
$domain = str_replace('www.', '', $domain);
// use one or more additional domains here
if ($domain == 'altsite.mobi' ||
    $domain == 'anothersite.net' ||
    $domain == 'thirdsite.com'
   ) domain_theme($domain);

You don't need to put your default domain in the last if-statement, this is just to verify that the domains you want to be themed are in fact set with their own theme folder which is named after the domain itself.

if you have lots and lots of domains with their own themes, you can change the last if-statement to read:

if ($domain !== 'defaultsite.com') domain_theme($domain);

That will result in errors unless you have a theme for each domain already uploaded.

If you want to allow multiple domains, but want to use the same theme for each domain, you can simply remove the lines containing TEMPLATEPATH and STYLESHEETPATH.

Drawbacks

If you don't have substantially different content on each of your domains Google will penalise you heavily.  You can avoid this by using a robots.txt file for your additional domains.  This is ideal for cases like mine, where you're only serving mobile content from the additional domain.

Preventing the Indexing of Duplicate Content

UPDATE: I've been asked to provide an example of how to block duplicate content using robots.txt files, so here's how I'm doing it:

I'm using two distinct robots.txt files.  One with the regular name, and another called robots_blocked.txt.  I then differentiate between them using .htaccess and mod_rewrite, information about which is infinitely googlable.

Here's what I have in my .htaccess file, directly above the wordpress section which should already be in there if you're using 'pretty' permalinks:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.)?yourmainsite\.com$ [NC]
RewriteRule ^robots.txt$ /robots_blocked.txt [NC,L]
</IfModule>

This basically ignores any requests for robots.txt via your main site, instead passing them to the real robots.txt file as they should be.  If you try to access robots.txt via any other domain (or subdomain) the rule is activated, and you're served robots_blocked.txt instead.

For the sake of the internet and your ranking in search engines, please ensure that any duplicate content (that is, the vast majority of your site) is properly excluded via your robots_blocked.txt

Fourteen Responses

  1. #1 16th of May, 2008 at 05:05

    Wow… that’s a great trick! Chapeau! ;-)

  2. #2 17th of May, 2008 at 02:59

    Bookmarked! It’s a clever trick :)

  3. #3 17th of May, 2008 at 03:24

    Cheers guys :)

  4. #4 25th of July, 2008 at 23:10

    You should check this out: http://striderweb.com/nerdaphernalia/features/virtual-multiblog/ - virtual multiblog is a regularly updated project dealing with this exact issue.  I use it to maintain about 10 different WP “installations” off of one set of files/themes/plugins.

  5. #5 25th of July, 2008 at 23:15

    That’s actually the exact reverse of what this does.

    This provides multiple domain names to the same content. Multiblogs provide different content with the same files.

    With a multiblog, you’d have to submit the same article twice, to display it for regular and mobile users.  With this, you just theme the same content differently.

  6. #6 1st of August, 2008 at 06:09

    Wow, excellent !
    I was seeking a while for a core solution building a multiple theme for a theme viewer solution. I thing I founded something :-)

    Do you think this could be used with an alias domain alias.mydomain.com serving an empty wordpress database for previeing my free templates ?

  7. #7 10th of September, 2008 at 10:34

    The solution is so cool, and you thought of avoiding duplicate contents as well, awesome! Thank you.

  8. #8 26th of September, 2008 at 18:45

    Great Article! Thank you very much!

    I’m using your solution to display my WP-blog in a mobile-version (m.weinschenker.name) and in a screen-enabled-version (www.weinschenker.name).

  9. #9 12th of October, 2008 at 12:21

    I exactly want which you have made.

    But I did not get it clearly.

    I have wordpress installed in : http://www.readgujarati.com/pustak with default theme.

    Now, I want to make mobile site. there for just now created one subdomain. like m.readgujarati.com

    after that do I need to install wordpress on that subdomain ?

    at present when i am opening my subdomain it is showing me blank page as that folder has no file at present.

    I am confused.

    please guide.

  10. #10 3rd of December, 2008 at 02:15

    [...] offer many solutions. I thought I had found an elegant work-around yesterday in the form of themed multiple domains in WordPress, which would allow me to have multiple domains pointing at a single instance of WordPress, wich [...]

  11. #11 13th of January, 2009 at 02:02

    This is a great post and like what others have already said, this is what i need except …

    After seeing Milorad’s replay I’m not sure which one is best for my blog. I have a blog setup on a subfolder (mistake but it is what it is …). I am using it to promote an Alex Jeffreys program. I am going to go into video marketing AND want to keep the same look and following that I currently have. I was just going to link the 2 together (one blog video, other for list building), but at times I’d like to incorporate the same information. So…..

    Should I multiblog OR just add the above code, link em together and be done with it? Probably obvious, but …

    Thankx
    Tim Buttles

  12. #12 18th of January, 2009 at 00:40

    Sorry guys, I am a ‘newbie’ to using WP blogs and would appreciate any help to my question above. I don’t want to be penalized by the SE’s and DON’T want to have to redo it again after the fact …

    Thankx

  13. #13 12th of February, 2009 at 17:25

    Should you change:
    RewriteRule ^robots.txt$ /robots_blocked.txt [NC,L]

    to:
    RewriteRule ^/robots.txt$ /robots_blocked.txt [NC,L]

    ?

  14. #14 17th of March, 2009 at 06:21

    Hi,

    Weird. I have done exactly what you write above. The code seams good. No PHP error but i get these redirect non stopping loop error:

    ERROR : Redirect Loop

    Firefox has detected that the server is redirecting the request for this address in a way that will never complete.
    The browser has stopped trying to retrieve the requested item. The site is redirecting the request in a way that will never complete.
    * Have you disabled or blocked cookies required by this site?
    * NOTE: If accepting the site’s cookies does not resolve the problem, it is likely a server configuration issue and not your computer.

Respond Now

Comments are Gravatar enabled

Fields marked * are required.

Allowed XHTML:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Close
E-mail It