Score:0

Subdomain redirecting me to main domain Apache2

in flag

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 [email protected]
        ServerName azaanjobs.com
        ServerAlias www.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/azaanjobs.com/fullchain.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/azaanjobs.com/privkey.pem
    </VirtualHost>
</IfModule>

<VirtualHost *:8080>
    ServerAdmin [email protected]
    ServerName government-jobs.azaanjobs.com
    ServerAlias www.government-jobs.azaanjobs.com.com
    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?

Score:0
in flag

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 [email protected]
    ServerName government-jobs.azaanjobs.com
    ServerAlias www.government-jobs.azaanjobs.com
    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 [email protected]
        ServerName azaanjobs.com
        ServerAlias www.azaanjobs.com *.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/azaanjobs.com/fullchain.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/azaanjobs.com/privkey.pem
    </VirtualHost>
</IfModule>

Notes:

  1. the updated configuration file replaces the incorrect www.government-jobs.azaanjobs.com.com 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

in flag
Thank you so much for the help. Subdomain is working now but when I change *:80 of main domain to azaanjobs.com:80, website isn't working.
in flag
You can have multiple sites defined with `*:80`. There's no need to change that. Apache will use the first matching reference.
in flag
Thanks for the answer. it really helped me.
in flag
I've created one more domain, it is not reading index.html file i changes the permissions also.
in flag
<VirtualHost cvmaker.azaanjobs.com:80> ServerAdmin [email protected] ServerName cvmaker.azaanjobs.com ServerAlias www.cvmaker.azaanjobs.com DocumentRoot /var/www/cvmaker/public_html/ <Directory /var/www/cvmaker/public_html> Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all </Directory> ErrorLog ${APACHE_LOG_DIR}/cverror.log CustomLog ${APACHE_LOG_DIR}/cvaccess.log combined
in flag
RewriteEngine on RewriteCond %{SERVER_NAME} =www.cvmaker.azaanjobs.com [OR] RewriteCond %{SERVER_NAME} =cvmaker.azaanjobs.com RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent] </VirtualHost>
in flag
<VirtualHost governement-jobs.azaanjobs.com:80> ServerAdmin [email protected] ServerName government-jobs.azaanjobs.com ServerAlias www.government-jobs.azaanjobs.com 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>
in flag
ErrorLog ${APACHE_LOG_DIR}/govterror.log CustomLog ${APACHE_LOG_DIR}/govtaccess.log combined RewriteEngine on RewriteCond %{SERVER_NAME} =www.government-jobs.azaanjobs.com [OR] RewriteCond %{SERVER_NAME} =government-jobs.azaanjobs.com RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent] </VirtualHost>
in flag
It is not reading any files present in /var/www/cvmaker/public_html/
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.