1

I'm trying to have 3 different domains and subdomains on my VPS:

  • pd.example.com
  • intranet.example.com
  • example.com

I have configured them, but for some reason each subdomain is getting redirected to the same index.html that is in the root directory of example.com

The screenshot for intranet.example.com:

intranet.example.com

The screenshot for example.com:

example.com

Here is the virtualhost for intranet.example.com:

<VirtualHost *:80>

    ServerAdmin soporte@example.com
    ServerName intranet.example.com
    ServerAlias www.intranet.example.com
    DocumentRoot "/var/www/intranet/"

    <Directory "/var/www/intranet/">

            Options Indexes FollowSymLinks
            AllowOverride all
            Require all granted

    </Directory>

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

    RewriteEngine on
    RewriteCond %{SERVER_NAME} =www.intranet.example.com [OR]
    RewriteCond %{SERVER_NAME} =intranet.example.com
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

Here is the virtualhost for example.com:

<VirtualHost *:80>

    ServerAdmin soporte@example.com
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example

    <Directory /var/www/example>

            Options Indexes FollowSymLinks
            AllowOverride all
            Require all granted

    </Directory>

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

    RewriteEngine on
    RewriteCond %{SERVER_NAME} =www.example.com [OR]
    RewriteCond %{SERVER_NAME} =example.com
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

Did I configure my virtualhosts correctly?

These are the 443 Virtual hosts for example.com:

    <IfModule mod_ssl.c>
    <VirtualHost *:443>

    ServerAdmin soporte@example.com
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example

    <Directory /var/www/example>

            Options Indexes FollowSymLinks
            AllowOverride all
            Require all granted

    </Directory>

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

    Include /etc/letsencrypt/options-ssl-apache.conf
    SSLCertificateFile /etc/letsencrypt/live/www.example.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/www.example.com/privkey.pem
    </VirtualHost>
    </IfModule>

This is for intranet.example.com:

    <IfModule mod_ssl.c>
    <VirtualHost *:443>

    ServerAdmin soporte@example.com
    ServerName intranet.example.com
    ServerAlias www.intranet.example.com
    DocumentRoot "/var/www/intranet/"

    <Directory "/var/www/intranet/">

            Options Indexes FollowSymLinks
            AllowOverride all
            Require all granted

    </Directory>

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

    Include /etc/letsencrypt/options-ssl-apache.conf
    SSLCertificateFile /etc/letsencrypt/live/www.intranet.example.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/www.intranet.lsgob.us/privkey.pem
    </VirtualHost>
    </IfModule>

.htaccess

    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^(.*)$ https://www.%{HTTP_HOST} [R=301,L]
  • Your screenshots appear to show that the main domain (example.com) is also redirected to the subdomain www.intranet.example.com (at least, that is what's showing in your screenshots) - or are your screenshots back to front? But your screenshots also show HTTPS - so we need to see your <VirtualHost *:443> containers. You also don't need to use mod_rewrite to redirect to HTTPS in a vhost context, you should use the simpler mod_alias Redirect. But don't you also want to canonicalise the www/non-www subdomain? – MrWhite Aug 29 '18 at 12:32
  • "each subdomain is getting redirected to the same index.html that is in the root directory" - Also, please clarify whether this is an external redirection (3xx response), as you suggest or simply that the same index.html document is being served? – MrWhite Aug 29 '18 at 13:57
  • @MrWhite that’s exactly the problem, I think. Somehow the same index.html is being used by the same domains but just that, as you can see on intranet.example.com the index is the same but the css folders are not, or at least are not working. About the redirections, those are configured on .htaccess to redirect to https and www so that way traffic is always on www and https – Alex Block Aug 30 '18 at 0:19
  • @MrWhite What do you suggest for redirections? Do you also need to see the *:443 virtual host files???? Regards! – Alex Block Aug 30 '18 at 0:20
  • Yes, you need to clarify the points above in your question. There's nothing you've shown above that explains the output you are seeing. Your screenshots show HTTPS, so yes, you need to include the relevant <VirtualHost *:443> containers in your question. And if you are also using .htaccess then please include these also. Since you have access to the server config and you are performing redirects in the server config then there does not seem to be any reason to do the same in .htaccess? Your screenshots are strange as they appear to show redirects going in opposite directions? – MrWhite Aug 30 '18 at 13:37
2

As mentioned in comments, I don't see any spurious redirects that you appear to be reporting and the two URLs do appear to return different content (although they "look similar"). So this appears to show different index.html documents, not the same?

Not the cause of the current problem, but your redirection in your <VirtualHost *:80> containers is unnecessarily complex and the non-www to www redirect in .htaccess is incorrect (you are removing the URL-path).

For example:

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

There is no need to check the SERVER_NAME in this <VirtualHost> container. The SERVER_NAME must already be one of the two values you are checking for otherwise it would never be at this point in the code in the first place.

And, unless you have plans to implement HSTS, then you might as well redirect to the canonical www subdomain (which you are doing later in .htaccess) in order to avoid a double redirect.

So, this could be simplified to a single mod_alias redirect:

Redirect 301 / https://www.example.com/

Repeat for your subdomains.

(Aside: Ideally, you would also have a separate <VirtualHost *:443> for the non-canonical non-www version of each domain that issues a simple redirect to the canonical www version. Although this does admittedly involve more code. Alternatively, you could use a mod_rewrite redirect in your existing <VirtualHost *:443> containers, similar to what you are doing in .htaccess.)

.htaccess

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST} [R=301,L]

This removes the URL-path from the request and always redirects to the document root, which I'm sure is not the intention. (It would be bad for SEO and besides, why are you capturing the URL-path in the RewriteRule pattern and not doing anything with it?)

You need to include the $1 backreference in the RewriteRule substitution, for example:

RewriteRule (.*) https://www.%{HTTP_HOST}/$1 [R=301,L]

You will need to make sure you have cleared your browser cache.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.