When adding subdomain it's redirecting back to main domain / Ubuntu 16.04 / Apache

December 9, 2016 4k views
Apache DNS Ubuntu 16.04

Having problem with my subdomain. Did everything by tutorial and now my subdomain is redirected to main domain. I do not have much experience with this kind of stuff so help would be appreciated. And main domain has SSL so maybe that's the problem or my hover settings for subdomain are wrong?

<VirtualHost *:80>
    DocumentRoot /var/www/
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined



Hover domain

@ A myIPadress  

*  A myIPadress     

mail  CNAME 

@ MX 10   

www CNAME  

3 Answers
verse December 9, 2016
Accepted Answer

So finally got it right. Now it's working.


www CNAME 

to  A myIPaddress

And added .htaccess in directory

RewriteEngine on

# redirect to https www
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)(domain\.com)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L]

# redirect to http subdomain
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^((?!www).+\.demo.domain\.com)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

Did you enabled your VirtualHost? As I see, you created new file for subdomain VH, if you did it - you need to enable it using a2ensite:

  • sudo a2ensite

Make sure to restart after any change you do to the config:

  • sudo systemctl restart apache2

Try to access subdomain again. If you still see wrong page, try clearing cache or going to Incognito mode.

  • Sorry forgot to add that, yes it's enabled by sudo a2ensite

    • Try to go on with NameVirtualHost. This is link from Apache docs, it very well explained and there are few examples.

      You can also look for logs at /var/log/apache2/error.log and /var/log/apache2/access.log. If you find any strange entries for subdomain, especially in error.log, it could give you mor insight on problem (or post it, we can probably help).

      • There is no error in error.log, read all and tried it nothing changed. I'm guessing that it might be problem with SSL? Is it possible that in place of 80 should be 443 port? Losing my mind here, tried everything that I could find and result is always the same.

Anybody? Need this resolved but have no idea what to try next. Tried everything that I could find... What am I missing?

Have another answer? Share your knowledge.