2

When using a sub domain it redirects to my default page/domain. This only happens when using HTTPS.. for example

https://forums.example.net - takes me to example.net ...BUT http://bans.example.net - takes me to the bans website (the correct site)

I'm using Apache 2 & Ubuntu 18.04 using my own VPS. ((I also have 2 more sub domains registered to the domain but i think it may be a problem with one of my Apache configs!)) or a Cloudflare problem.

SITES ENABLED CONFIG

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

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

<VirtualHost *:80>
    DocumentRoot /var/www/html/example.net/
    ServerName example.net

    <Directory /var/www/html/example.net/>
        Options +Indexes +FollowSymLinks +MultiViews +Includes
        AllowOverride FileInfo Options
        Order allow,deny
        allow from all
    </Directory>
RewriteEngine on
RewriteCond %{SERVER_NAME} =example.net
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot /var/www/html/bans.example.net/
    ServerName bans.example.net

    <Directory /var/www/html/bans.example.net/>
        Options +Indexes +FollowSymLinks +MultiViews +Includes
        AllowOverride FileInfo Options
        Order allow,deny
        allow from all
    </Directory>
RewriteEngine on
RewriteCond %{SERVER_NAME} =example.net [OR]
RewriteCond %{SERVER_NAME} =bans.example.net [OR]
RewriteCond %{SERVER_NAME} =forums.example.net
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

SSL CONFIG INSIDE THE SITES-ENABLED

<IfModule mod_ssl.c>
<VirtualHost *:443>
    DocumentRoot /var/www/html/example.net/
    ServerName example.net

    <Directory /var/www/html/example.net/>
        Options +Indexes +FollowSymLinks +MultiViews +Includes
        AllowOverride FileInfo Options
        Order allow,deny
        allow from all
    </Directory>


Include /etc/letsencrypt/options-ssl-apache.conf
ServerAlias forums.example.net
ServerAlias bans.example.net
ServerAlias status.example.net
SSLCertificateFile /etc/letsencrypt/live/bans.example.net-0001/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/bans.example.net-0001/privkey.pem
</VirtualHost>
</IfModule>
<IfModule mod_ssl.c>
<VirtualHost *:80>
    DocumentRoot /var/www/html/forums.example.net/
    ServerName forums.example.net

    <Directory /var/www/html/forums.example.net/>
        Options +Indexes +FollowSymLinks +MultiViews +Includes
        AllowOverride FileInfo Options
        Order allow,deny
        allow from all
    </Directory>


</VirtualHost>
</IfModule>
<IfModule mod_ssl.c>
<VirtualHost *:80>
    DocumentRoot /var/www/html/bans.example.net/
    ServerName bans.example.net

    <Directory /var/www/html/bans.example.net/>
        Options +Indexes +FollowSymLinks +MultiViews +Includes
        AllowOverride FileInfo Options
        Order allow,deny
        allow from all
    </Directory>


</VirtualHost>
</IfModule>
1
RewriteCond %{SERVER_NAME} =bans.example.net [OR]
RewriteCond %{SERVER_NAME} =status.example.net [OR]
RewriteCond %{SERVER_NAME} =example.net [OR]
RewriteCond %{SERVER_NAME} =forums.example.net
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

Change SERVER_NAME to HTTP_HOST. Or, look for UseCanonicalName On in your server config and change this directive to Off.

When UseCanonicalName On is set then the SERVER_NAME server variable contains the hostname from the ServerName directive, otherwise, it is the same as HTTP_HOST, ie. the hostname from the HTTP request.

If you are inside a vHost then you probably don't need to check for the requested hostname? You may not even need to use mod_rewrite here at all - a simple mod_alias Redirect may suffice.

However, there are other inconsistencies in your server config... (Maybe some of this is due to over exemplification?!)

  • Your first code block is seemingly chopped in the middle of a vHost container?
  • You have multiple vHost configs for the same port and server name. Only one can win. One has the redirect logic; the other doesn't?
  • You are checking for requested hosts that are seemingly impossible? eg. in the bans.example.net:80 vHost, the redirect is also checking against example.net and forums.example.net?
  • You are wrapping vHost:80 in <IfModule mod_ssl.c>?
| improve this answer | |

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.