How to: multiple blogs, one WordPress installation

When you have a weblog and play with it a little you will quickly gather more of them. For production, development, maybe testing and possibly various domains. Then upgrading WordPress to a new version becomes a lot of work. It would be a lot easier if you would only need one WordPress installation with their own database and upload folder. This is WordPress multi user, or multi site or domain if you prefer. It is not hard to achieve. Just follow this tutorial.

Choose multi site or domain

While they may seem the same, there is an essential difference between these. Multi site would be set up as: www1.domain.com and www2.domain.com where multi domain would be setup as www.domain1.com and www.domain2.com. You could even have these types mixed but that would make it a little more complicated. For now we assume you would like to host www.domain1.com and www.domain2.com.

Install the WordPress files

Just install WordPress like you would for one website. The point all domains / sites to the folder where you installed it.

Create a config file, here comes the magic

In the config file you determine which domain is requested and select the database (and if needed database user, server and password) based on that. For the site version use $parts[0] instead of $parts[1].

$host = $_SERVER['HTTP_HOST'];
$parts = explode('.',$host);
if ($parts[3] = "") {
    $domain = $parts[0];
} else {
    $domain = $parts[1];
}

switch ($domain) {
case "domain1":
    $db = "database1";
    break;
case "domain2":
    $db = "database2";
    break;
}

define('DB_NAME', $db);
define('DB_USER', 'user');
define('DB_PASSWORD', 'password');
define('DB_HOST', 'hostname');

Create the databases

WordPress needs you to create the empty database before you set it up. If you point to a domain of which the database is not created yet, you will be informed WordPress can select the database. When the database is there, you get the setup screen.

Finalize

The make sure things don’t get mixed up you should change some of the default settings in the admin panel. At Options > Miscellaneous set the upload folder to wp-content/uploads/domain/ and don’t forget to create the corresponding folder.

About Martijn Stegink

IT guy convinced about the power of mobility and the web - having used it since 1993 with the rise of Mosaic. Fiddling with Mac's when not reading, gardening or herding his small flock of children.
This entry was posted in Wordpress and tagged . Bookmark the permalink.

50 Responses to How to: multiple blogs, one WordPress installation

  1. Max says:

    Actually, I am using this method to have multiple wordpress for one installation.

    http://me.mywebsight.ws/2006/08/11/host-multiple-wp-sites-on-one-installation/

    But I found that if I have 100 sites, the list of table becomes very long. Since they put all the sites just in one db.

    Your method seems to put different sites in different dbs. So all I need is create the db and edit the following if I want to have more sites:

    case “domain1″: $db = “database1″; break;

    Am I correct?

    Thanks

  2. Max says:

    Hello,

    I tried to add your code into wp-config.php, but it seems I can’t get it works. When I go to the site, there is nothing shows.

  3. rusty says:

    Hello !

    I would like to use your plaintextemplate but unfortunatly there are some errors when I validate it…

    Bye !

  4. Max: with the method you mention not only the list of tables will be long, also you wil encounter problems with plugins that use tables, I believe. You are correct in your assumptions but I would not suggest that for 100 sites either. For that, I would create a ’sites’ database with one table that holds domain and database name. The config file would check the referring domain and the get the right database name from the sites database. As for your test, did you change all variables?

    Rusty: I’ll check the theme. It’ll help if you leave your e-mail for contact…

  5. Rusty: i fixed the XHTML and CSS errors. Shame on me for not proper checking earlier. Thanks for the heads up. The download on the theme pages is updated and now version 1.1.

  6. Hallo Martijn,

    This post is fantastic!! I am going to try it. I have questions starting from my not so good meaning of English. Sorry!

    1) Using the code you mentioned above I must use http://www.mydoman.com / http://www.mydomainagain.com / etc…? I cannot use sub.mydomain.com of http://www.mydomain.com/sub. Isn’t it?

    2) When I will have multiple-blogs I will be very happy to have one common static frontpage where to show one or two entries from each sub-blog. Is it possible?

    Thank you so much in advance. Tiziano

  7. Thank you. As for your questions:

    1. You could vary either the first part of the FQN by using myfirstsite.domain.com, mysecondsite.domain.com or use different domains. Using /sub should be possible too but you’d have to do some coding.

    2. That is possible but I guess would require extensive coding. However it seems they sort of do that at WordPress.com. An easier approach for this would be using the rss feeds of the sites and display this on one page. I’ve seen some pages do that already.

  8. Martijn,

    Thank you so much for your reply. I have found my way using the WP-o-Matic plugin. This plugin allows me to automatically write posts starting from RSS pulled-out by another website. For my needs, I will use WP-o-matic on the “mother” website importing RSS entries from all the “child” websites.

    Have a nice day. Tiziano

  9. Martijn says:

    Glad you got it your way. Thanks for sharing the plugin info with me. Always good to find something new.

  10. Atuyo says:

    Hi, how is this method different from using WordPress MU?

  11. I have no idea, never check MU. However this option you can use and continue to upgrade every new version without problem, with MU that may be different? You tell me.

  12. John says:

    Great tip.

    I’m building a blog network with all blogs on their own domain.

    I would like each domain to have a unique IP but hosted on the same platform.

    Is this hard to achieve? Thanks.

  13. Optonotes says:

    I just wanted to say thank you for posting this. It helped me out quite a bit.

  14. John, your machine would need to have all the IP addresses you want. If you do that the rest would be the same as in my article. Thanks Optonotes.

  15. Thanks, have some 40 blogs – will try it today. Why doen’t you release it as a hack?

  16. Scott says:

    What If I want to use one common database instead of all separate databases?

  17. That can be done. Instead of the above use this:

    $host = $_SERVER['HTTP_HOST'];
    $parts = explode('.',$host);
    if ($parts[0] == "www") {
        $db = $parts[1];
    } else {
        $db = $parts[0];
    }
    
    $table_prefix  = $db . '_';  
    

    That will add all tables to one database with different prefixes so you can still see which tables belong to which site.

  18. chris says:

    I’m having problems with this. It works for the domain I installed it on, but when I go to setup another domain I just get an “Error establishing a database connection” message.

  19. Martijn says:

    Hey Chris. You have to create a database with the name of the domain. So if the site is on http://www.domain1.com the database should be called domain1. Same for the sub domain approach where the database should be named after the sub domain.

  20. Pingback: Tutorial Wordpress - Como Instalar Múltiplos Blogs em um único Banco de Dados? - Wordpressbr.com

  21. Mary Ann says:

    Is there a plug in that accomplishes the multiple-blog on one domain issue? (Thanks for the tutorial, btw).

  22. I don’t quite understand what you mean Mary Ann. Maybe you could elaborate a bit?

  23. Martijn, This looks to be exactly what I am seeking. I have a few questions I hope you can answer.

    1. I run on a VPS and have been installing new cPanel accounts for each new site and then installing WP. With this method, would I simply create the new sites as “Add On” sites to my main domain account, or do I still create a whole new setup for each client?

    2. I am slightly confused by what you mean by pointing all domains to the file where it was installed… can you please explain that further?

    3. I plan to have many sites, so going back to the comment about 100 or more sites, you mentioned having a DB setup to store the wp database info for each site. I love that idea, but in application, how would I modify your code to pull that info from that db? (sorry, but most of my db setup is done already by scripts, so I am not sure how to manually create)

    Thanks in advance… I am really hoping this will be the solution I need

  24. Let me clarify, I can set up the db for the information… it’s the calling of the information to fit your code that I need assistance on….

  25. Pingback: Hosting multiple blogs on a single WordPress installation » Paul Maunders | Web log

  26. efoc says:

    My problem is how to make two or more domains point to one WordPress? I am using DirectAdmin. Should I do something in DNS?

  27. Matt Perry says:

    This is a really really great and simple solution. Excellent! Thank you!

  28. Don says:

    I did something similar, but had issues with updating plugins and with plugins that wanted to create files outside the site’s upload folder.

    The updating problem was that a plugin could update itself and the database for that site, but the other sites would out of sync as their database wouldn’t fit the plugin any more.

    I found that by setting the php variable WPCONTENTDIR to a unique folder for each site (in my case, “/content/sitename/”) it helped but meant that I have to upload plugins and templates multiple times.

    I also can’t seem to get wp-super-cache to work at all. Any ideas on getting that to work would be great, as I can’t seem to even get it to work for 1 site, never mind all of them. :(

  29. Tim M says:

    Doesn't work for me. Pasted:


    $host = $SERVER['HTTPHOST'];
    $parts = explode('.',$host); if ($parts[3] = "") {
    $domain = $parts[0];
    } else { $domain = $parts[1]; }

    switch ($domain) { case "http://www.domain.com": $db = "DB_name"; break; case "domain2": $db = "database2"; break; }

    define('DB_NAME', $db);

    *** GET Error establishing a database connection

    Tim M

  30. James says:

    Well, apparently I'm too dumb to make this work! Would you be willing to pass a couple e-mails back and forth about my test? I've setup a primary test environment (test.dirkon.com) as well as subdomains test2 and test3 and a site test.icebox500.com . Each has their own DB on the test site but when I messed with the config I got no reaction. I'm sure I missed something in your instructions… The config file is back to normal now

  31. Dave says:

    Thanks for the information! A couple of questions (likely obvious):

    1) I want to manage blogs on multiple domains. I assume that the solution you propose will eliminate the hassle of having to update and manage the codebase for multiple blogs, but that it is not intended to address adminstering each of those blogs from a single admin interface. 2) Will each blog show up under its own domain? Will the fact that the code is being pulled from a single domain negatively impact SEO in any way? 3) Can I hire you to assist me?

  32. Jason Belec says:

    Anyone managed to get this to work on 2.7.x? Doesn't seem to recognize the IP coming in, resulting in it being lost and not having access to the DB.

    Bueller, Bueller, Bueller, Anyone?

  33. Rufas says:

    You script doesn’t work, so I modified it a bit:

    $domain = strtolower($SERVER["SERVERNAME"]);

    switch ($domain) { case “www.domain1.com”: $db = “database1″; break; case “www.domain1.com”: $db = “database2″; break; }

    define(‘DB_NAME’, $db);

    define(‘DB_USER’, ‘user’);

    define(‘DB_PASSWORD’, ‘password’);

    define(‘DB_HOST’, ‘localhost’);

    And now: it works — and it is on WordPress 2.8.4 !

    However there’s a some problems:

    1) permalinks: WordPress, by default, doesn’t use that. If you make a new blog in this way, all post will become “page not found”, because of .htaccess file is modified.

    2) non-www and www URL problem: can’t seems to be able to redirect domain1.com to http://www.domain1.com . Gives “Error establishing a database connection”

    3) sitemap.xml: need to be renamed for each blog, or else it will be overwritten.

    Regards, Rufas

  34. @Rufas: my script does work, in fact this site runs with it. However in your example the domains are the same; they should be different. So if you’d use http://www.domain1.com the database name would be domain1 and for http://www.domain2.com the database name would be domain2.

    As for the problems you mention:

    1) By default WordPress will indeed overwrite the htaccess. If you use the same setting for all your sites, that’s fine. If not it is possible to -manually- specify different rules for different domains; Try searching for ‘htaccess for multiple domains’.

    2) This is why I use the code below. If it is domain.com it will pick the first part of the url, if it is http://www.domain.com it will pick the second part. if ($parts[3] = “”) { $domain = $parts[0]; } else { $domain = $parts[1]; }

    3) Good point, hadn’t thought about that.

  35. Rufas says:

    Thanks for the replying, I’ll try that later.

    By the way, I’m sorry about the database part – that was a typo. It should be:

    switch ($domain) { case “www.domain1.com”: $db = “database1″; break; case “www.domain2.com”: $db = “database2″; break; }

    Thanks for pointing out.

  36. Rufas says:

    At last I did it. I found the culprit of the problem: two missing underscores “_” in the line:

    $host = $_SERVER['HTTP_HOST'];

    No wonder it won’t work.

    Thank you for your wonderful script.

  37. Ah, that was a problem with formatting of the code in the article. Sorry for that, it’s now corrected.

  38. lucy says:

    wp-hive plugin multi-site, multi-domain

    http://wp-hive.com/

  39. Pingback: WordPress : plusieurs blogs sur une seule installation

  40. Irfan Mansha says:

    Hi,

    I am working on multiple wordpress sites by one installation. I created all tables in one database. I use unique table_prefix for every website.

    Problem is that i had used a template for a page on domain1 and it was working fine. I also created a permalink for it.

    But when i created second website (domain2) and tried to access the page of domain1, i found error “The page is not found”.

    Kindly let me know what’s the problem.

    Thanks,

  41. Irfan Mansha says:

    “Hi,

    I am working on multiple wordpress sites by one installation. I created all tables in one database. I use unique table_prefix for every website.

    Problem is that i had used a template for a page on domain1 and it was working fine. I also created a permalink for it.

    But when i created second website (domain2) and tried to access the page of domain1, i found error “The page is not found”.

    Kindly let me know what’s the problem.

    Thanks,”

    I have uploaded local .htaccess file on online server and every thing got fine.

    I have created another site to test the situation and problem come back. I again uploaded local .htaccess file on online server and everything got fine again.

    I think .htaccess file is involved in this issue. Kindly tell me the reason.

    Thanks

  42. Froriavax says:

    Various of people blog about this topic but you wrote down some true words.

  43. Vivek says:

    You said “point all domains / sites to the folder where you installed it. “

    but how ?

    I mean i have added all the domains to my host and point all domain to same dns settings ? is there anything left in this ?

  44. Mark says:

    Great listing here, i found that “Choose multi site or domain, Install the WordPress files, Create a config file”, here comes the magic in word press this is nice and fantastic..many thanks to share this information nice post.

  45. Joe Angelo says:

    Thanks for the post. I did some googling on this topic and this is the code I use from some other site:

    if ($SERVER["HTTPHOST"] == ‘domain1.com’) { define(‘DBNAME’, ‘databe-1′); } elseif($SERVER["HTTPHOST"] == ‘domain2.com’){ define(‘DBNAME’, ‘database-2′); }else { define(‘DB_NAME’, ‘database1′); }

    This works for sub-domains as well. I’m not a programmer though so I don’t know which one is better.

  46. multiblog does apparently that, more or less.

  47. Syech says:

    Ok, Martin. The first is: this tutorial is very nice. And according to its explaination, I assume you are a great tutor.

    But the second: I’m affraid I can’t use this method. because of my purpose to create multiple SUBdomain (sorry If I capitalize the word ‘sub’. It was meant to emphasized it). Which means, I want to install wordpress in 3 subdomains, for example: http://example.com/subdomain1 , http://example.com/subdomain2 , and http://example.com/subdomain3 . And then I want the recent posts from subdomain2 and subdomain3 appear on subdomain1. After I read your response to Tiziano Luccarelli’s comment above, I guess this method is not compatible with my purpose. Right?

    So if you, or someone else who read this comment, know what the best method for my purpose, will you let me know?

    Thanks before.

  48. @syech: No, my solution does not provide that, better check wpmu, it can do what you want.

  49. @william: if you do not understand the database talk, better check out wp-hive as I think that does what you want without having to go into code.

  50. james mino says:

    You wouldnt believe how long ive been googling for something like this. Went through 5 pages of Google results without finding anything. Very first page on Bing. There was this…. Really have to start using it more often!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>