Subdomain redirects to the main domain

Posted September 14, 2017 9k views
ApacheDigitalOceanUbuntu 16.04

I know this question has been asked at least a million times, but I tried all the solutions or recommendations that I could find and it still doesn’t work.
At first, I followed this tutorial How To Set Up Apache Virtual Hosts on Ubuntu 16.04. On top of that, I created A record in domain panel with a hostname of pointing to my droplet.
I noticed shows my blog project, which is on main domain or exactly running on Nginx.
I checked my /etc/hosts file and added an entry like this: blog blog localhost

My virtual host file looks like this:

<VirtualHost *:80>

        DocumentRoot /var/www/

        <Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride all
                Order allow,deny
                allow from all
                Require all granted

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


# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

I used a2ensite command to enable the site. I tried using the command with .conf extension and without. I disabled default .conf file (000-default or something like that).

I tried adding .htaccess file in my project directory with this in it:

RewriteEngine on

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

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

Paths to the document root are correct and double-checked. Restarted apache2 service, droplet more times than I can count of.
I don’t know what should I do anymore. Maybe I am missing something? Is it maybe because my index file is .php file?

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.

Submit an Answer
1 answer

What is your primary web server, Apache or NGINX?

Are you trying to run both?

As you have provided you Apache file(s) can you provide your NGINX configuration as well.