14

How do i offer Basecamp/SAAS App like sub domains to clients?

For example, my website URL is http://www.example.com whenever the user signup i want to offer them url like http://company.example.com which should load files/contents from /app directory of the website.

And later i want them to choose their own domain/sub-domain via CNAME, so that they can have URL like http://clients.mywebsite.com

I want to do this in Pure PHP and .HTACCESS and offcourse everything happens automatically.

And i want to keep URL structure same.., that is http://company.example.com/login, http://company.example.com/accounts, http://company.example.com/files/style.css though these files are located inside /app directory i want them to be accessed like this.

I can handle all PHP functions, i need help with .htaccess codes and how to go about CNAME.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Dhruv Kumar Jha
  • 6,421
  • 8
  • 37
  • 48

1 Answers1

12

Make sure DNS resolves all subdomains by including following in domain's zone:

*.example.com. 14400 IN A HereBeIP

Next, configure web server to recognize all incoming requests, for Apache, you need to add following to VirtualHost:

ServerAlias *.example.com

That's it. The only thing remaining is the logic in the code, you need to check host name to see whether a request was done to example.com or foo.example.com.

Updated with more details.

So, assuming your files are in /path/to/files/app, you need to configure virtual host like this:

<VirtualHost>
    SeverName example.com
    ServerAlias *.example.com
    DocumentRoot /path/to/files/app
    # plus what else is needed
</VirtualHost>

This way all requests will resolve to /app directory. To test it, apply above DNS configuration or simply add the domains you want tested into your hosts file, /etc/hosts on Linux or c:\Windows\System32\drivers\etc\hosts on Windows.

David Kuridža
  • 7,026
  • 5
  • 26
  • 25
  • How do i do this with .HTACCESS, i mean whenever a request is sent (http://company.example.com) php will display page accordingly, but i want to keep the URL structure same. – Dhruv Kumar Jha Jan 08 '11 at 08:43
  • in short, you cant with just .HTACCESS –  Jan 08 '11 at 09:11
  • As Dagon said, you can't do it with .htaccess. Make sure to apply above settings, what's left is just Apache configuration to point to the same structure. – David Kuridža Jan 08 '11 at 09:19
  • if your on a shared hosting account your probably our of luck, get a dedicated server instead. –  Jan 08 '11 at 09:26
  • For now i am using it on Development server, i will use Dedicated server later on, what i want to know is how will i get the files from /app directory when a sub domain is visited/opened – Dhruv Kumar Jha Jan 08 '11 at 12:41