Apache subdomain redirects to main domain

9,859

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 [email protected] (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.conf It looked like this:

<IfModule mod_ssl.c>
<VirtualHost *:443>
        ServerAdmin [email protected]
        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 www.mysite.com 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

Share:
9,859

Related videos on Youtube

Bwookzy
Author by

Bwookzy

Updated on September 18, 2022

Comments

  • Bwookzy
    Bwookzy over 1 year

    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 [email protected]
        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 [email protected]
        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

    • John Orion
      John Orion over 6 years
      well its hard to tell where you have to go in this example ... if the files were at a folder called /var/www/forum you would go to www.yoursite.com/forum but because there are periods in the path .. not sure if you would have to go to www.yoursite.com/forum.example.com
    • Bwookzy
      Bwookzy over 6 years
      @JohnOrion The main part of the site points to the directory /var/www/html so I don't see where it would become a sub directory. I could try to move it to /var/forum if you would like. edit: I just changed the dir to /var/forum/html and it is still happening.
    • John Orion
      John Orion over 6 years
      no .. you would have to put the files in say /var/www/html/forum or something and you would access them by going to www.yoursite.com/forum you would also have to edit the conf ` <Directory /var/www/html/forum> and leave out document root ... unless you are trying to have it as www.yoursite.com goes to the main html .. and forum.yoursite.com goes to the forum .. if that is the case .. i would hve to look more into how to accomplish that
    • Bwookzy
      Bwookzy over 6 years
      yes! that is what I am trying to do. I want to have my forum on a subdomain of the website, and not a sub directory of the main website. the problem is that the subdomain is redirecting, or displaying the main domain instead
    • John Orion
      John Orion over 6 years
      ok sorry to clarfy ... you want to have it www.yoursite.com and the normal site and forum.yoursite.com as the forum address? sorry .. just want to be sure i think i have the answer to that
    • Bwookzy
      Bwookzy over 6 years
      yes, www.example.com is the normal site with just plain html, and then forum.example.com for php forums and stuff. I already have the forum setup on the www.example.com site but I want to migrate it.
    • Bwookzy
      Bwookzy over 6 years
      damn. Well maybe someone else here can help me with my issue. I'm new to apache, and especially dns stuff because before I was hosting straight off of an ip, which I wanted to change and get ssl stuff. thanks for the help anyway.
    • John Orion
      John Orion over 6 years
      ok .. i think i made a room .. you can join me here
    • John Orion
      John Orion over 6 years
      ok .. guess its not working .. you can read up on this setting-up-subdomain-on-ubuntu-server but he uses examples with periods in the name too .. dont do that .. make your files in a normal folder name like /var/www/html as your main .. and /var/www/forum as the subdirectory and use those in teh conf instead of www.example.com because as i mentoned .. if you do get it working .. example.com is your main domain so the access address would be forum.example.com.example.com
    • John Orion
      John Orion over 6 years
      they more i try it .. i get the same result as you .. now that i have my DNS set if i try to access john.mysite.com ... it just goes to mysite.com or www.mysite.com ... I see your issue now ... working on how to resolve it
  • Bwookzy
    Bwookzy over 6 years
    thanks for this, I will test this out once I figure out how to fix apache2 bc it just randomly stopped working on me. can't connect to any of my IPs or my DNSs. (this happened before i added your stuff so don't worry)
  • John Orion
    John Orion over 6 years
    ahh bummer .. have you checked the error.log after trying to start the server .. it may give you some answers /var/log/apache2/error.log usually by default
  • John Orion
    John Orion over 6 years
    if you have time .. you can connect to that link in the other replies i gave that says .... join me here .. i still have the room active .. maybe I can trouble shoot for you
  • Bwookzy
    Bwookzy over 6 years
    no, askubuntu says that i need atleast 20 rep to talk there :(
  • John Orion
    John Orion over 6 years
    ahh damn :( sorry .. well did you look into the log.. Is apache2 running .. have you tried to restart it in a terminal sudo service apache2 start and see if it throws any errors or tells you to check something? or if its already running.. maybe sudo service apache2 stop then the above command to start it again
  • Bwookzy
    Bwookzy over 6 years
    Haha! Figured out why apache wasn't starting. It was damn docker for some reason hooking to port 80 before apache could. Uninstalled that sh*t because I don't need it.
  • John Orion
    John Orion over 6 years
    cool glad to hear .. did you have any luck with getting the setup working
  • John Orion
    John Orion over 6 years
    if you want me to try to help if you are still having difficulties .. let me know .. maybe we can use google hangouts or something to chat since you cant connect to the rooms here