josipr95
By:
josipr95

Subdomain redirects to the main domain

September 14, 2017 297 views
DigitalOcean Apache Ubuntu 16.04

Hello,
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 evidenca.josiprajkovic.com pointing to my droplet.
I noticed evidenca.josiprajkovic.com 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:

127.0.1.1 blog blog
127.0.0.1 localhost
165.227.146.42 www.evidenca.josiprajkovic.com evidenca.josiprajkovic.com

My virtual host file looks like this:

<VirtualHost *:80>

        ServerAdmin josipr95@gmail.com
        ServerName evidenca.josiprajkovic.com
        ServerAlias www.evidenca.josiprajkovic.com
        DocumentRoot /var/www/evidenca.josiprajkovic.com/public_html/

        <Directory /var/www/evidenca.josiprajkovic.com/public_html>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride all
                Order allow,deny
                allow from all
                Require all granted
         </Directory>

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

</VirtualHost>

# 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?

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.

Have another answer? Share your knowledge.