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:

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

  1. the updated configuration file replaces the incorrect value for ServerAlias with a proper .com reference
  2. a *.azaanjobs.com alias was added to the main domain to ensure any "unexpected" traffic is captured and routed to a probable location
  3. 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
  4. the subdomain is still listening on port 8080, so this will need to be updated to *:80 before 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 👍🏻

9

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like