skip to Main Content

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


  1. 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:

    <VirtualHost *:80>
        ServerName domain.com
        ServerAlias www.domain.com
        ServerAdmin [email protected]
        DocumentRoot /var/www/vhosts/domain.com/public_html
    
        <Directory /var/www/vhosts/domain.com/public_html>
            Options -Indexes +FollowSymLinks
            AllowOverride All
        </Directory>
    
        ErrorLog /var/www/vhosts/domain.com/error.log
        CustomLog /var/www/vhosts/domain.com/requests.log combined
    </VirtualHost>
    
    <VirtualHost *:80>
        ServerName site1.domain.com
        ServerAdmin [email protected]
        DocumentRoot /var/www/vhosts/domain.com/public_html
    
        <Directory /var/www/vhosts/domain.com/public_html>
            Options -Indexes +FollowSymLinks
            AllowOverride All
        </Directory>
    
        ErrorLog /var/www/vhosts/domain.com/error.log
        CustomLog /var/www/vhosts/domain.com/requests.log combined
    </VirtualHost>
    
    <VirtualHost *:80>
        ServerName site2.domain.com
        ServerAdmin [email protected]
        DocumentRoot /var/www/vhosts/domain.com/public_html
    
        <Directory /var/www/vhosts/domain.com/public_html>
            Options -Indexes +FollowSymLinks
            AllowOverride All
        </Directory>
    
        ErrorLog /var/www/vhosts/domain.com/error.log
        CustomLog /var/www/vhosts/domain.com/requests.log combined
    </VirtualHost>
    
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search