Reputation: 2539
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.
Upvotes: 0
Views: 433
Reputation: 1336
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
Upvotes: 1
Reputation: 151
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.
Upvotes: 1
Reputation: 9616
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>
Upvotes: 2