My subdomain showing me main website?

July 21, 2019 340 views
Ubuntu 18.04 DigitalOcean

Im trying to create a subdomain and the main domain is a wordpress site!

Whenever i try to configure as shown in digitalocean tutorials and same in all other tutorials to create a subdomain, my subdomain shows me the content of my main domain.

Eg: my main domain is “https://example.com” and i want to create “https://forum.example.com” but it shows me my root site

I edit the vhosts as per the tutorials but still no luck, take a look
vhost file:

<VirtualHost *:80>
ServerAdmin info@example.com
ServerName hi.example.com
DocumentRoot /var/www/hi.example.com/public_html

    <Directory /var/www/hi.example.com/public_html/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

Please someone help me, im trying this from weeks!
Appreciate your help!

1 Answer

Hello,

The Vhost looks correct. What you might be missing is:

  • First make sure that the file permissions and ownership in the /var/www/hi.example.com/ folder are setup correctly so that the Apache user has read and write access to the files
  • Then as this is a wordpress website, make sure to run a search and replace for your database for https://yourdomain.com to be replaced with https://hi.yourdomain.com (you could do that for both http and https)
  • Then check your .htaccess file and make sure that there are no redirects there

Note: of course before running the search and replace make sure to backup your database in case anything goes wrong!

Hope that this helps!
Bobby

  • Will you please explain me these points:

    1) First make sure that the file permissions and ownership in the /var/www/hi.example.com/ folder are setup correctly so that the Apache user has read and write access to the files

    2)Then check your .htaccess file and make sure that there are no redirects there

    Appreciate your help

    • By default if you have not changed the Apache user it should be www-data. So you could run this command to make sure that Apache has access to your files:

      sudo chown www-data:www-data -R /var/www/hi.example.com
      

      Regarding the .htaccess file just check the content of the file and look for any Rewrite rules to the main site. So you could run:

      cat /var/www/hi.example.com/public_html/.htaccess
      

      Also another thing, if you are using a caching plugin for your Wordpress website make sure to clear the cache.

      Hope that this helps!
      Bobby

      • I tried this

        sudo chown www-data:www-data -R /var/www/hi.example.com
        

        and changed every user which was root:root but still nothing and about the .htaccess i dont have any in the https://hi.example.com, I just have a index.html to test it but i still get the main website!!

        Please if there’s anything more you can do please help

        • A few questions:

          • Have you restarted apache after the changes that you made to the Vhost?

          • Where exactly did you add the Vhost? You need to make sure that it is enabled and you can see it at ‘/etc/apache2/sites-enabled/‘

          Let me know how it goes!
          Bobby

          • Yes, I restarted the Apache Server after changing the hosts

            I used this command to add the vhosts:

            
            sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/hi.example.com.conf
            

            and yes i can see it in ‘/etc/apache2/sites-enabled/‘

            I even changed their user to www-data

          • The next step in this case would be to do the following:

            • First make sure that the content of the /etc/apache2/sites-enabled/hi.example.com.conf is correct

            • Try accessing the hi.example.com subdomian a few times via an incognito browser and then check your apache error log:

            tail -100 /var/log/apache/error.log
            

            Let me know how it goes!
            Bobby

          • I did checked the error log but no error was being filed

            i dont know what is happening…im frustated please help

          • Hello,

            Can you provide me with the content of the /etc/apache2/sites-enabled/hi.example.com.conf file so that I could make sure that it is correct and it matches the one in the sites-available?

            Also can make sure that the file ends in .conf otherwise it would not work.

            Another thing that I think could be causing the issue is, are you accessing the site on http or https?

            Regards,
            Bobby

      • I have my site on https, yaa right now what I did is I destroyed the server and created a new one

        So my first site that is main Wordpress website was installed easily due to one-click, but when i tried for the second site using vhosts it again shows the same thing, my main website.

        So what i noticed is the default user for my main website is www-data and when i create using vhosts the user is root:root, so maybe this is the reason, what do you think?

        Is there any way i can set the user to www-data by default so that i can create vhosts using that user!!

        Again, appreciate your help!!
        Thankyou

        • What happens if you change the owner and the group to www-data of your Vhost files? Does this fix the issue? If not, there’s no need to worry about it as it should work by default with root:root.

          Can you confirm if you are visiting your subdomain name via https or http? As if you do not have a Vhost for your subdomian for https, that would be the reason why you are getting the main site’s content. To fix that you would also need a Vhost for https for your subdomain or you need to visit it via http only.

          Regards,
          Bobby

          • Ya, im using the domain via https, so what should i do to create a vhost for https??

            You are the best man!!

          • Hehe no problem at all man! Yes you need to create a Vhost for https (port 443) and then this should all work as normal.

            Let me know how it goes!

            Regards,
            Bobby

          • So i got this from the internet, tho i have some questions

            <VirtualHost *:80>
            
                ServerName cloud2help.com
            
                ServerAlias www.cloud2help.com
            
                ServerAdmin webmaster@cloud2help.com
            
                DocumentRoot /var/www/cloud2help.com/public_html
            
            
            
            
                <Directory /var/www/cloud2help.com/public_html>
            
                    Options -Indexes +FollowSymLinks
            
                    AllowOverride All
            
                </Directory>
            
            
            
            
                ErrorLog ${APACHE_LOG_DIR}/cloud2help.com-error.log
            
                CustomLog ${APACHE_LOG_DIR}/cloud2help.com-access.log combined
            
            RewriteEngine On
            
            RewriteCond %{HTTPS} !on
            
            RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
            
            </VirtualHost>
            
            <VirtualHost *:443>
            
                ServerName cloud2help.com
            
                ServerAlias www.cloud2help.com
            
                ServerAdmin webmaster@cloud2help.com
            
                DocumentRoot /var/www/cloud2help.com/public_html
            
            
                <Directory /var/www/cloud2help.com/public_html>
            
                    Options -Indexes +FollowSymLinks
            
                    AllowOverride All
            
                </Directory>
            
                ErrorLog ${APACHE_LOG_DIR}/cloud2help.com-error.log
            
                CustomLog ${APACHE_LOG_DIR}/cloud2help.com-access.log combined
            
            SSLEngine on
            
            SSLCertificateFile  /etc/ssl/certs/cloud2help.pem
            
            SSLCertificateKeyFile /etc/ssl/certs/cloud2help.key
            
            </VirtualHost>
            

            1) Can i add the new vhost in the same file of port 80 or should I create a new file for https

            2) And im using cloud flare for my sites, so what should i do about this

            
            SSLEngine on
            
            SSLCertificateFile  /etc/ssl/certs/cloud2help.pem
            
            SSLCertificateKeyFile /etc/ssl/certs/cloud2help.key
            

            Thankyou

          • Hello,

            Just one question before you make the changes, is your subdomain also going through CloudFlare? As if you are doing your SSL termination on CloudFlare then maybe the https Vhost would not be needed.

            Regards,
            Bobby

          • Yes my subdomain is also going through https from cloudflare, and one more thing when i turned off my SSL from cloudflare for my second site “example.com” it went right. And when I turned SSL on it again showed me my main website.

            So i think the problem is the https, so can you tell me what do for that??

            And what does this mean “As if you are doing your SSL termination on CloudFlare then maybe the https Vhost would not be needed.”?

            Thankyou

          • Hello,

            Basically what you need to do is to get an SSL certificate for your two sites, then create a Vhost for port 443 for both sites and add the SSL files.

            Here’s a really good article that I found:

            https://readybytes.in/blog/how-to-setup-free-ssl-certificate-using-cloudflare

            It shows all of those steps.

            Let me know how it goes!
            Bobby

          • I dont know how to thank you, you are god mannnnn.

            I cant express how happy i am, after trying this for so many time, this finally worked. My site is up and running!!!!

            Thankyou so much for everything❤❤

          • No problem at all! I’m happy that it is all working at the end!

            Make sure to follow me on twitter :)

            https://twitter.com/bobby_iliev1

            Regards,
            Bobby

          • I'ld love to, but im not really a social media person, Sorry

            Thankyou again!!

Have another answer? Share your knowledge.