5

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

3 Answers3

4

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.

Dale Fraser
  • 4,394
  • 7
  • 37
  • 71
3

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.

Yisroel
  • 8,144
  • 4
  • 24
  • 26
2

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>
speeves
  • 1,308
  • 9
  • 10
  • 2
    Just keep in mind that I can edit my HOSTS file such that dev.example.com points to your production IP and your server may think it's running the dev version. – Raymond Camden Oct 26 '11 at 01:36
  • See this blog post about why I use CGI.HTTP_HOST instead of CGI.SERVER_NAME http://www.n8williams.com/devblog/coldfusion/get-the-domain-entered-from-the-request – speeves Oct 26 '11 at 01:41
  • Good point, CF Jedi Master! In my case, it probably doesn't matter, but would obviously matter if I were running some sort of code that exposes sensitive information in dev, (or has a bug that can be exploited. – speeves Oct 26 '11 at 01:44