6

I have following virtual host ( in apache2.conf file ) to load all subdomain from a single directory on AWS

<VirtualHost *:80>
    DocumentRoot /var/www/html
   ServerName mydevsite.com
   ServerAlias mydevsite.com

</VirtualHost>

<VirtualHost *:80>
    DocumentRoot /var/www/html/apps
    ServerName mydevsite.com
    ServerAlias *.mydevsite.com
</VirtualHost>

It not working and subdomains are still pointing to html directory

I tried following in htaccess as well but then it gives me 500 error

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.website\.com$
RewriteCond %{HTTP_HOST} ^(\w+)\.website\.com$
RewriteCond %{REQUEST_URI}:%1 !^/([^/]+)/([^:]*):\1
RewriteRule ^(.*)$ /%1/$1 [QSA]

My simple requirement is to load main domain from root directory and all subdomains from apps directory

  • It's possible that giving both hosts the same ServerName may cause an issue. Try using a different name (eg apps.mydevsite.com) for the second host. – USD Matt Dec 21 '17 at 10:35
  • @USDMatt but in that case it will point only mentioned host for say apps.mydevsite.com will point to apps folder but other like new.mydevsite.com will not. – Vikram Dec 21 '17 at 10:37
  • as I didnt try it and as a newbie I am assuming it will work as I explained, correct me if it works other way – Vikram Dec 21 '17 at 10:38
  • I meant change the ServerName directive but keep ServerAlias *.mydevsite.com, which should cause that virtual host to be used for all other subdomains. – USD Matt Dec 21 '17 at 12:37
3

Your apache2.conf has the same ServerName mydevsite.com for both vhosts. Apache wants distinct names to identify each virtual host. The configuration could look like this:

<VirtualHost *:80>
    ServerName mydevsite.com
    DocumentRoot "/var/www/html"
</VirtualHost>

<VirtualHost *:80>
    ServerName apps.mydevsite.com
    ServerAlias *.mydevsite.com
    DocumentRoot "/var/www/html/apps"
</VirtualHost>

In doubt please read Apache HTTP Server Version 2.4 - Using Name-based Virtual Hosts.

Before restarting Apache test the configuration:

$ sudo apache2ctl configtest

If configtest quits without an error, restart Apache:

$ sudo systemctl restart apache2.service

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.