logo

Using MySQL To Host Multiple Websites in One Database

Recently I created a MySQL database for a CMS for websites that promote "venues". Each of these websites could be accessed via any number of domain names and I needed a way to tie incoming URL request to the right Venue.

To match a request to a venue I added a table called "Domains", which looks like this:

image

Notice how each of the 4 venues has multiple web addresses, but always only has one preferred address.

In PHP we can use the variable $_SERVER["HTTP_HOST"] to find the domain of the incoming request. We can then find the target Venue from the above table by using the following SQL:

SELECT venues.*, domains.domain as preferred_domain
FROM venues 
INNER JOIN domains ON venues.venue_id=domains.venue_id
WHERE venues.venue_id = 
        (SELECT venue_id FROM domains WHERE domain='www.royaloakpub.org') 
AND domains.preferred=1;

The trick here is the nested SELECT statement inside the WHERE clause. A new one on me. My approach to mastering SQL is to assume everything must be possible. This one had me stumped at first for a while, but I persisted and found the solution in the end.

The above SQL tells us - for each request - what Venue we're dealing with and, also, what the preferred domain for that Venue is.

By running the above query in PHP you can assign a value to a  PHP variable called $VenueID and then, for all subsequent queries to the CMS, you can append the Venue's ID to the WHERE clause. All tables in the CMS I made have a column called venue_id. So, for example, here's the SQL to get all the photos for one of the venues:

SELECT *
FROM photos
WHERE photos.venue_id = 2;

As an example of using this approach, notice how www.royaloakpub.org serves the same content as www.peakpub.co.uk. Notice in each site's HTML source that the canonical link meta-tag always points to the preferred domain, which is peakpub.co.uk in this case.

Ideally each Venue's website should always have a single domain name in use, but, you know how it is, it's never that simple is it? To get round the negative SEO effect in this case I used a meta tag but I could just as well have done a 301 redirect if the preferred domain weren't used in the original request.

Comments

    • avatar
    • Tom
    • Wed 5 Jan 2011 03:29 AM

    "To get round the negative SEO effect in this case I used a meta tag but I could just as well have done a 301 redirect if the preferred domino weren't used."

    You may have moved on from Domino, but your typin' fingers haven't yet.

      • avatar
      • Jake Howlett
      • Wed 5 Jan 2011 03:34 AM

      Why do people keep saying I've moved on from Domino?

      I can see why but I don't want people thinking I don't "do" Domino any more and not come to me for having work done.

      Show the rest of this thread

    • avatar
    • Palmi
    • Wed 5 Jan 2011 07:15 AM

    you could always do some domino Tips off and on ;)

    • avatar
    • Matt
    • Wed 12 Jan 2011 12:32 AM

    FYI - subselect can get very slow in mysql less then 5.5

Your Comments

Name:
E-mail:
(optional)
Website:
(optional)
Comment:


About This Page

Written by Jake Howlett on Tue 4 Jan 2011

Share This Page

# ( ) '

Comments

The most recent comments added:

Skip to the comments or add your own.

You can subscribe to an individual RSS feed of comments on this entry.

Let's Get Social


About This Website

CodeStore is all about web development. Concentrating on Lotus Domino, ASP.NET, Flex, SharePoint and all things internet.

Your host is Jake Howlett who runs his own web development company called Rockall Design and is always on the lookout for new and interesting work to do.

You can find me on Twitter and on Linked In.

Read more about this site »

More Content