Subdomain Redirecting Me to Main Domain Apache2
I Have a Website Running on Apache Server. When I Try to Visit a Subdomain, I Am Redirected to the Main Domain. Here Is the Apache Configuration File...
I have a website running on Apache server. When I try to visit a subdomain, I am redirected to the main domain.
Here is the Apache configuration file:
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerAdmin
ServerName azaanjobs.com
ServerAlias
DocumentRoot /var/www/azaanjobs/public_html
<Directory /var/www/azaanjobs/public_html/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/
SSLCertificateKeyFile /etc/letsencrypt/live/
</VirtualHost>
</IfModule>
<VirtualHost *:8080>
ServerAdmin
ServerName government-jobs.azaanjobs.com
ServerAlias
DocumentRoot /var/www/government-jobs/public_html/
<Directory /var/www/government-jobs/public_html>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
How can I fix this?
1 Answer
Apache will try to match a web request with the configured domains in the order they appear in the file system and within the configuration files. As a general rule of thumb, it's better to have subdomains handled before the main site, which should be configured to act as a "catch-all" for any traffic that is unhandled.
With this in mind, your configuration file could be updated to look like this:
<VirtualHost *:8080>
ServerAdmin
ServerName government-jobs.azaanjobs.com
ServerAlias
DocumentRoot /var/www/government-jobs/public_html/
<Directory /var/www/government-jobs/public_html>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerAdmin
ServerName azaanjobs.com
ServerAlias *.azaanjobs.com
DocumentRoot /var/www/azaanjobs/public_html
<Directory /var/www/azaanjobs/public_html/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/
SSLCertificateKeyFile /etc/letsencrypt/live/
</VirtualHost>
</IfModule>
Notes:
- the updated configuration file replaces the incorrect
value forServerAliaswith a proper.comreference - a
*.azaanjobs.comalias was added to the main domain to ensure any "unexpected" traffic is captured and routed to a probable location - there does not seem to be any configuration on the main site for
*:80, which could be an issue if the something in front of the server is not converting non-SSL traffic to use SSL - the subdomain is still listening on port
8080, so this will need to be updated to*:80before visitors can see the site without specifying the port in their browser
Remember to restart Apache after changing the configuration file:
sudo service apache2 restart
This should give you what you need 👍🏻