How to serve multiple domain name websites with one CakePHP application

Say you have a need to deliver multiple websites which have same structure but each site is served on different domain names and have different content.

For example, perhaps you are selling a product in many different countries and you want to have a separate website for each country.

This solution allows you to use one CakePHP application and one MySQL database to serve multiple websites each with different domain and content.

The domains need to be hosted on shared hosting which allows multiple domain hosting and allows some way of pointing multiple domains to a single application or route.

You would create your CakePHP application as normal but modify it as per this solution so that it can identify the domain the request is coming from and then serve up content for that domain only!

The modifications are summarized here and the complete PHP code is in Github:

    • Create a table called ‘domain’ (or whatever you want with as many columns as you need) that will hold domain specific values that are retrieved to be used in in application. At a minimum it should have the each site’s domain so you can do a lookup to match it to the domain being requested.
    • Add domain table foreign key (domain_id) to other tables so that they can be filtered by domain when retrieving data. (This means likely you will require additional steps to ensure that the foreign key is written to your tables. How you do that is up to you but for example if you allow users to create accounts or post comments then you can simply record the domain_id (for example you CakePHP Configure::read value when new user or comment is added.)
    • Create a new function  (I called min ‘getDomainSettings‘ ) in the CakePHP AppController beforeFilter section. This function will read domain name from current site visitor request and use it as parameter to filter database records to just that domain’s values. I used the built-in CakePHP Configure::write variables for these domain values that can be used throughout the CakePHP application.
    • Add optional conditions when retrieving model data using these CakePHP Configure:write variables in other CakePHP controllers’ to retrieve specific model records for the current domain and as well as modify view presentation based on model records. Lots of potential here, you could also swap out css styles or CakePHP themes based on domain and make sites totally different.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top