Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to run multiple sites on same codebase in ColdFusion base on domain or subdomain

I have a ColdFusion application. I would like to be able to run the same codebase (rather than duplicate it) against multiple domains or subdomains. Each of the sites would be essentially the same, except that they would be branded, skinned and have different titles, etc.

So, what I'm looking for is how to have: www.abc.com and www.xyz.com and beta.mycompany.com all running off same codebase. Ideally, it will be quick to add new domains as new clients sign on.

I've seen this question for PHP and Rails, but not CF. Here is what I was thinking (and it seems to work), but was wondering if there are would be performance issues or a cleaner suggestion.

IN APPLICATION.CFC


<cfif cgi.server_name EQ "www.abc.com"  >
    <cfset request.client_id=1>
<cfelseif cgi.server_name EQ "www.xyz.com">
    <cfset request.client_id=2>
... etc             
<cfelse>
    This application not configured.
    <cfabort>   
</cfif>

Now, just key everything off client_id...

like image 827
user1013736 Avatar asked Oct 26 '11 00:10

user1013736


2 Answers

The application instance is based on the Application.name

so you just name each instance differently

In application.cfc you can have something like this

<cfcomponent>

    <cfset this.name = replace(CGI.HTTP_HOST, '.', '', 'all') />

Each domain now causes a different application name, thus separate instance and sets of application variables etc.

like image 78
Dale Fraser Avatar answered Sep 21 '22 18:09

Dale Fraser


I do something similar, but I keep all the info in a database. That makes it much easier to add new websites, and doesn't require any code changes for each new client or template.

Heres my code from application.cfc:

<cffunction name="OnApplicationStart">
  <cfset application.websites = structNew()>

  <cfquery name="sites">
    SELECT websiteID, url FROM websites
  </cfquery>

  <cfloop query="sites">
    <cfset application.websites["#url#"] = CreateObject("component", "websites").init(websiteID)>
  </cfloop>
</cffunction>

Now I have a collection of websites the application is configured to respond to. Each site loads its template. The templates are also saved in the database, so each site can be easily configured to any template.

For each request, we just need to find the correct website:

<cffunction name="OnRequestStart">
  <cfargument name="targetPage">

  <cfif structKeyExists(application.websites, cgi.SERVER_NAME)>
    <cfset request.website= application.websites["#cgi.SERVER_NAME#"]>
  <cfelse>
    <cfabort>
  </cfif>

  <cfset request.template = request.website.template>
</cffunction>

Now each request has the website and template available througout.

I'm use this to run 3 ecommerce sites with 3 different templates off one codebase and database.

like image 35
Yisroel Avatar answered Sep 23 '22 18:09

Yisroel