Using Application.cfc to route dns wildcards

441 views Asked by At

I'm using a godaddy coldfusion shared hosting account and I want to provide my users friendlier/direct urls. I've added a wildcard to my domain so all subdomains point to the same root folder.

How do I use just the Application.cfc to route dns wildcards to their appropriate destinations?

index.cfm in the root folder will just list cities Going to city.domain.com should bring up domain.com/city/index.cfm if it exists and domain.com/index.cfm if it doesn't.

I tried cflocation but I think it went into an infinite loop.

3

There are 3 answers

0
Mark Mandel On BEST ANSWER

You could always do a server side redirect, and hide it all away from the client.

So regular requests go to index.cfm (www.domain.com)

And then if there is a city (in onRequestStart()):

city = ListFirst(CGI.SERVER_NAME, ".");
if(!StructKeyExists(request, "forward") AND (city != "www" || city != "domain"))
{
   request.forward = 1;
   getPageContext().forward("/#city#/index.cfm");
   abort;
}

I put the request scope variable 'forward' in there to stop potential infinite loops, as the Application.cfc is called again.

You can see more details here:

http://www.compoundtheory.com/?action=displayPost&ID=26

http://download.oracle.com/javaee/6/api/javax/servlet/jsp/PageContext.html#forward%28java.lang.String%29

3
Dan Short On

You need to make sure that you're checking for the right thing in CGI.SERVER_NAME. Something like this should do the trick:

<cfif ListLen(CGI.SERVER_NAME, ".") NEQ 2 AND ListFirst(CGI.SERVER_NAME, ".") NEQ "www">
    <cflocation url="http://domain.com/#ListFirst(CGI.SERVER_NAME, ".")#/index.cfm" />
</cfif>
0
JhnSctt On

It sounds like your existing code would work fine if placed at the top of root/index.cfm. You could supplement that with onMissingTemplate() in Application.cfc to handle 404 situations.