Using Different Databases With a Single WordPress Installation

January 4, 2012

In an effort to make designing new websites easier and to reduce the amount of multiple wordpress installs I have running on my test server at any given time, I made some changes to my wordpress test server setup.

I have been using a single install per website for the past few years, mostly because it was the easiest solution.

When I start a new website, I copy over my root wordpress installation that has my base theme and plugins in it, then customize the config file. I know it is certainly not the most efficient way to do things, but it works and sometimes, taking the time to work out a new way is simply not practical.

However, in an effort to make things a little more condensed, I decided to do some playing with using a single wordpress installation for multiple websites. I am sure this has been done many times already and there are probably a number of resources available, but I usually like to figure these things out, for the most part, on my own.

Playing With a WordPress Network

I tried the built in Multi-Site Option first, to see how it worked, because I had never played with it. Setup is very easy and it basically did what I needed. It is a cool feature and makes creating a network of related wordpress sites really easy.

However, it uses a single database, creating a new set of tables for each new site. So, for example, when you add a new site, a set of tables with “wp_2_” as the prefix.

Ultimately, when I launch a client’s website, I will dump it and then upload it to their website host, I did not want to have to figure out how to convert the mult-site database into a single site database.

I am sure it is probably not too difficult and there may even be tools involved, but I can see several issues, especially with shared settings, so for development I would still prefer to have a single database for each website.

Creating a Custom WordPress Network

So, with the multi-site database structure not being exactly what I was going for, I decided to use the same htaccess rules, but simply create a dynamic wp-config.php file.

Within the config file, I use a simple if/else statement to check the REQUEST_URI variable and dynamically set the database name, based on the page request.

A basic example is below:


if(stristr($ref, "website_1") !== false)
define('DB_NAME', 'website_1');
else if(stristr($ref, "website_2") !== false)
define('DB_NAME', 'website_2');

In the above example, PHP’s case-insensitive search function is used to load the correct database based off page request. If the page request includes, website_1, the website_1 database is used, ect.

I may go back and modify this a little more, to check position too, as there is a small risk of name collision using this method.

With the modified .htaccess rules shown below, which are the same as the default multi-site rewrite rules, requests to wp-includes, wp-admin, and other important folders are correctly redirected, no matter the requested ‘folder’.

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]

# uploaded files
RewriteRule ^([_0-9a-zA-Z-]+/)?files/(.+) wp-includes/ms-files.php?file=$2 [L]

# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^[_0-9a-zA-Z-]+/(wp-(content|admin|includes).*) $1 [L]
RewriteRule ^[_0-9a-zA-Z-]+/(.*\.php)$ $1 [L]
RewriteRule . index.php [L]

Using Custom Upload Directories

The only other hurdle was making sure each website was using a different uploads folder, as I did not want clients to be uploading files to the same directory or to have to manually remove/clean unused them each time.

I overcame this issue by changing the default upload directory under settings->media. Instead of using the default one, I changed it to something like “wp-content/uploads_1”

Potential Problems with This Setup

I currently have two different test sites setup with this config and everything seems to work well, of course only time will tell.

One potential issue is that plugins and themes are shared, so users may potentially be able to see all the current themes installed. This could cause issues if you were trying to hide themes/plugins from other clients.

There is a small chance of a name collision too within the config file, as if someone created a page called “website_2/website_1,” it would load the wrong database, so I will probably change the string search portion of the config file in the near future, probably checking the location.

Setting a custom upload directory is also not ideal, but it is not a huge deal and I may change it at a later time or when I actually launch the websites.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s