Reputation: 417
I can't seem to figure this one out. I have several sites hosted on Ubuntu 14.04/LAMP and I have a .conf file for each domain I'm hosting.
However, if someone enters a non-existent subdomain, for example, of one of the sites on the server, apache just redirects you to a random site (seems to find the first .conf file in /etc/apache/sites-enabled in alphabetical order).
When I have a site only configured for <VirtualHost *:80>
(non-SSL) and someone (like a Google bot) requests the https version of this site, they are redirected to the first .conf file with <VirtualHost *:443>
rules defined.
How can I prevent this? I just want everyone to land on an error page if a subdomain of a given domain doesn't exist, or if the non-https version of the site doesn't exist (since, apparently you can't redirect https to http).
Is there something specific I can put in 000-default.conf and 000-default-ssl.conf to prevent this?
Thanks!
Upvotes: 1
Views: 2025
Reputation: 417
Here's what I did to fix this. Added zzz.conf (so it's read last in alphabetical order in /etc/apache2/sites-enabled/). This just leads to a directory with a file that says "Error."
Here's what I put in zzz.conf:
<virtualhost *:80>
ServerAlias *
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</virtualhost>
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerAlias *
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
SSLEngine on
SSLCertificateFile /etc/apache2/ssl/server.crt
SSLCertificateKeyFile /etc/apache2/ssl/server.key
<FilesMatch "\.(cgi|shtml|phtml|php)$">
SSLOptions +StdEnvVars
</FilesMatch>
<Directory /usr/lib/cgi-bin>
SSLOptions +StdEnvVars
</Directory>
BrowserMatch "MSIE [2-6]" \
nokeepalive ssl-unclean-shutdown \
downgrade-1.0 force-response-1.0
# MSIE 7 and newer should be able to use keepalive
BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown
</VirtualHost>
</IfModule>
Upvotes: 2