Question

Subdomain redirects to the main domain

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?


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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

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.