Virtual host not working AWS apache

amazon-web-servicesApache2mod-rewritevirtualhost

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

Best Answer

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

Related Topic