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

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	


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

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:

  1. sudo a2ensite

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

  1. sudo systemctl restart apache2

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

The following steps worked for me. 1: add virtual host like the following with chmod 664 and chown www-data:www-data

<VirtualHost *:80>
        DocumentRoot /var/www/subdomain
        <Directory /var/www/subdomain/>
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted

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

        <IfModule mod_dir.c>
            DirectoryIndex index.php index.cgi index.html index.xhtml index.htm

RewriteEngine on
RewriteCond %{SERVER_NAME}
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

2: sudo add-apt-repository ppa:certbot/certbot (add the repository) 3: sudo apt install python-certbot-apache (Install Certbot’s Apache package with apt)

4: type “certbot5: select the newly created virtual host 6: and you are all good to go. cheers

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel