user1013736
user1013736

Reputation: 51

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...

Upvotes: 5

Views: 1721

Answers (3)

Yisroel
Yisroel

Reputation: 8174

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.

Upvotes: 4

Dale Fraser
Dale Fraser

Reputation: 4758

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.

Upvotes: 4

speeves
speeves

Reputation: 1376

Yes, that would work. I throw it into my Application.cfc. I used this to set different Application variables for DEV and PROD.

Application.cfc:

     <CFIF not CGI.HTTP_HOST EQ "www.example.com" AND not CGI.HTTP_HOST EQ "example.com">
       <CFSET Application.Environment = "Dev">
       ...
     <CFELSE>
       <CFSET Application.Environment = "Prod">
       ...             
     </CFIF>

Upvotes: 2

Related Questions