|Best Practice CNAME's|
I am setting up a website, which will have different CNAMES. i.e:
Not having much experience with this before, I want to find out the best practice for serving different content once the browser detects client1.test.com, as oppose to client2.test.com once I have set the CNAME records in my host providers control panel.
I have been advised that .htaccess can point these urls to different holding folders, i.e. instead of client1 serving the generic content the index for that page may sit in the www.test.com/client/ directory. Is this how CNAME's are supposed to be setup, or have I missed the general idea.
Thanks in advance.
The first question is why are you using CNames instead of simply using an 'A' record, which is faster on DNS lookup.
The purpose of a CName is that it is an alias -- It points to another domain's DNS record, possibly on a different DNS server, so that it serves as a 'soft link' to the IP address defined in that other DNS record. If the pointed-to DNS record is changed, no change in the CNAME record is required. However, the other result is that clients (e.g. browsers and search robots) have to do two DNS requests to find the IP address to which the current HTTP request should be sent (assuming that this DNS lookup isn't already cached locally).
The disposition of hostname requests (resolving them to a specific filespace on a server) is unrelated to their DNS configuration, so the one really has nothing to do with the other. Therefore, the answer to "What's the best practice?" is "Do whatever you like with them."
Your purpose for defining "client1" and "client2," not some aspect of the DNS configuration, should drive your decisions.
If you have server config access privileges, the best approach (assuming that you're hosting client (customer) sites here) is to set up individual VirtualHosts for each client, and keep these clients' files in completely-separate filespaces.
Lacking config access, then yes, .htaccess can be used to resolve requests for different hostnames to different subdirectories (or to the same subdirectory, if desired). However either way, there is nothing to keep client 1 from examining/modifying client 2's files if FTP access is granted to these clients. Therefore, their site security is non-existent with this model.
See the Apache "virtual hosting" tutorials for more information of virtual hosts, and see the "mass virtual hosting" example in the Apache URL Rewriting Guide for an example of the ".htaccess method."
There are also scores (if not hundreds) of previous threads on this subject posted here, findable using a site search for "rewrite subdomain subdirectory" and similar. Add "RewriteCond HTTP_HOST" to the search keywords to further narrow that search to mod_rewrite methods.