Apache subdomain redirects to main domain

I have an apache server that I am trying to setup, yes I am on the newest version. I want to have a main domain as a website, then I want to have a sub domain as a forum for members. I have the sub domain setup, and I also have the conf file setup inside of the apache etc directory. I don't know what I am doing wrong it just redirects me to the main page of my website instead of going to the .html page I setup for testing.

Here is the code of my .conf file:

<VirtualHost *:443> # ServerName forum.example.com ServerAdmin ServerName forum.example.com ServerAlias forum.example.com DocumentRoot /var/www/forum.example.com <Directory /var/www/forum.example.com> Options -Indexes +FollowSymLinks +MultiViews AllowOverride All Require all granted </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *:80> # ServerName forum.example.com ServerAdmin ServerName forum.example.com ServerAlias forum.example.com DocumentRoot /var/www/forum.example.com <Directory /var/www/forum.example.com> Options -Indexes +FollowSymLinks +MultiViews AllowOverride All Require all granted </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

I do not have an .htaccess file AFAIK

10

2 Answers

OK .. finally figured out the issues.. got it to work

first of all on your host dns ... you need to add the record for your server name I use namecheap.com .. I added a A + Dynamic DNS Record with a host john and my ip address ... same as my A record for www ... I'm assuming its just a standard A Record anywhere else

once that was there .. it may take a while before you can actually ping it once you can .. you will be good to go if all your other settings were correct

Ok now for the configuration files i used.. I created two in sites-available..

the first one was called 001-john.conf I found out that it is important to have the file end in .conf or you wont be able to use a command that seemed to fix the issue.

In that configuration I added the following :

<VirtualHost *:80> ServerAdmin (of course you would use your domain name instead of mysite.com) ServerName john.mysite.com ServerAlias john.mysite.com DocumentRoot /web/john (of course you would make the root the path to the folder where the files are stored)
<Directory /> Options FollowSymLinks ExecCGI AllowOverride ALL
</Directory>
<Directory /web/john/> Options ExecCGI FollowSymLinks Includes Indexes (or whatever directives you want) AllowOverride ALL order allow,deny allow from all
</Directory>
ScriptAlias /cgi-bin/ /web/plus/cgi-bin/ (if you wanted to create a cgi bin use your correct path for this)
<Directory "/web/plus/cgi-bin"> AllowOverride ALL Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch Order allow,deny Allow from all
</Directory>
ErrorLog /var/log/apache2/errorjohn.log
CustomLog /var/log/apache2/accessjohn.log combined
</VirtualHost>

the second configuration file was for ssl and i called it 001-john-ssl.confIt looked like this:

<IfModule mod_ssl.c>
<VirtualHost *:443> ServerAdmin ServerName john.mysite.com ServerAlias john.mysite.com DocumentRoot /web/john
<Directory /> Options FollowSymLinks AllowOverride ALL
</Directory>
<Directory /web/john/> Options Indexes FollowSymLinks Includes MultiViews ExecCGI AllowOverride ALL Require all granted
</Directory>
ScriptAlias /cgi-bin/ /web/plus/cgi-bin/
<Directory "/web/plus/cgi-bin"> AllowOverride ALL Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch Order allow,deny Allow from all
</Directory> LogLevel warn ErrorLog /var/log/apache2/errorjohn.log CustomLog /var/log/apache2/accessjohn.log combined SSLEngine on SSLProtocol all -SSLv2 SSLCipherSuite ALL:!ADH:!EXPORT:!SSLv2:RC4+RSA:+HIGH:+MEDIUM SSLCertificateFile /etc/apache2/ssl/your.crt file SSLCertificateKeyFile /etc/apache2/ssl/your.key file SSLCACertificateFile /etc/apache2/ssl/your.ca-bundle SetEnvIf User-Agent ".*MSIE.*" nokeepalive ssl-unclean-shutdown
</VirtualHost>
</IfModule>

once that was all saved I ran

sudo a2ensite 001-john.conf

and

sudo a2ensite 001-john-ssl.conf

Then I ran

sudo service apache2 reload

Then to check to see if it had both .. which before it didnt before .. I just created links from site-available to site-enabled .. that didnt seem to work for me but after running a2ensite with the files labeled with the .conf extention..I ran apachectl -t -D DUMP_VHOSTS and had this result:

VirtualHost configuration:
*:443 is a NameVirtualHost default server mysite.com (/etc/apache2/sites-enabled/000-default-ssl.conf:2) port 443 namevhost mysite.com (/etc/apache2/sites-enabled/000-default-ssl.conf:2) port 443 namevhost john.mysite.com (/etc/apache2/sites-enabled/001-john-ssl.conf:2) alias john.mysite.com
*:80 is a NameVirtualHost default server mysite.com (/etc/apache2/sites-enabled/000-default.conf:1) port 80 namevhost mysite.com (/etc/apache2/sites-enabled/000-default.conf:1) port 80 namevhost john.mysite.com (/etc/apache2/sites-enabled/001-john.conf:1) alias john.mysite.com

**just as a note .. my original site is located in directory /web/public and the subdomain site is located at /web/john... since yours was located at /var/www/html I would probably use something like /var/www/forum for you subdomain folder to store your forum files in and i would use forum.yoursite.com as the ServerName and ServerAlias but hopefully with the example you will see the format and setup of everything.

once the dns records got updated from namecheap .. i was able to access john.mysite.com and it would show the simple "under construction" file in there and if i just went to it would give me my normal page that was setup a while ago

so now its working with a subdomain

Hopefully this will help you through and set things up properly .. most likely its because the configuration files probably didnt have the .conf extensions and you didnt use a2ensite to enable them.. that seemed to be the problem i was running into

8

As a hint for people having the same problem: In my case adding a SSL certificate to the Subdomain fixed the problem. My main domain runs on SSL and obviously all HTTP traffic was redirected to HTTPS on main domain.

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