I would like to build many subdomains that ultimately download code from one directory.
Examples:
site1.domain.com
, site2.domain.com
,
site3.domain.com
should download the code from the site directory located inside the directory containing the website files, i.e. www/site.
I use php
, apache
and .htaccess
.
I prepared the following code:
RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteCond %{HTTP_HOST} !^domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^([^.]+).domain.com$ [NC]
RewriteRule ^(.*)$ /site/$1 [L]
Unfortunately it does not work. Is there anything else I should add? E.g. configure the site.domain.com subdomain and set DNS (A record) for the main domain? I would be grateful for help or a link to the tutorial.
Only site.domain.com
works (shows code from this directory). Other subdomains are not displayed (the website does not exist).
2
Answers
The proper way would be to create in Apache several virtual hosts pointing to the same directory.
In your virtual hosts
.conf
file (see where to locate) add, copy or update the virtual hosts to something like this:Your question is vague, it is not clear what your actual setup is. So we can give only hints into what aspects you need to take care of:
You definitely need DNS resolutions for those subdomains. Two alternatives: either you configure individual records for each one, or you use a "wildcard" record that points all subdomains to the same address. But you definitely do need one of the two.
As @ITgoldman correctly points out you can setup "virtual hosts" inside your http server for those domains. That allows for individual settings if required. If you have no use for that you can rely on the "default host" the apache http server uses if there is no exact match for the requested host name in form of a virtual host: the first configured host always is used as the default host.
A last resort, especially if you do not have control over the server configuration, might be the use of distribued configuration files (typically called ".htaccess"). But that cannot be used as a substitute for the steps above. If you need to implement any rewriting rules at all (for what?), then you can do it in there. But then the target in such a rewriting rule should point to the same directory according to your question. Not to different folder as it currently does in your attempted implementation.