I’ve been reading previous posts but I can’t figure out what will work or wont work with my setup so any help here is much appreciated!
For starters, I’m using the latest release of CakePHP (2.2). I’m hosting on a (gs) account w/MediaTemple.
I’ve got a site where a user can signup for an account at domain.com.
The user can access and change their info at dashboard.domain.com.
The user can view their individual information at sites.domain.com/username. The user can change the appearance of this page by selecting a theme from the database and for this, I’m using CakePHP’s theming structure. There’s one basic layout with multiple css options, so everything is pulled from the database into this one file and depending on which ‘theme’ they choose, the different css file is pulled. That all works well.
What I’d prefer is that they see their page from a wildcard subdomain(?), ie. username.domain.com instead of sites.domain.com/username.
I’d also like to give the option for them to register their own domain names, and point/redirect/something to the subdomain while still showing their unique domain in the url. I’d also like to not have to host additional domain names so can this be done w/name servers?
Any thoughts on where I go from here? Thanks in advance 🙂
This is a very common request, and it falls under the "Multi-tenancy" approach to web application development.
I answered a question recently that uses this approach.
Code
The linked answer shows the DomainRoute that I use to support dynamic subdomains on the CakePHP side of things. This has all the code you need to handle it from that point of the project.
The only applicaiton specific code in there, and the code you will need to change is the
Causerelated stuff:Causeis a Model in my application, and it has adomainExistsmethod. This method checks to see if the passed$domainexists on anyCause(or sub-website). The implementation ofdomainExistsis a very simplefindcall:If the domain does exist in the database, it adds the full domain to the
$paramsvariable.If the domain does not exist in the database, it does some redirection.
DNS
The DNS should be setup as a wildcard. The way this is done differs from provider to provider, but in ordinary BIND, its done like this:
That adds a wildcard CNAME to your DNS records.
I hope that helps. The code from the previously answered question is the most helpful, so ensure you read through that carefully first.