Question

How do you redirect an HTTP request with a nonexistent subdirectory to HTTPS 404?

I’m having a similar issue as the user here. I’m running an Ubuntu 14.04.4 droplet and using Apache2 for the webserver.

My current redirect setup goes like this:

The last one is where the problem is. For some reason, my permanent HTTP to HTTPS redirect fails if a subdirectory or file is also part of the URL. On Chrome, I get redirected to a ERR_CONNECTION_REFUSED page for example.comdoesnt-exist (obviously not a real domain) over HTTPS. For that last case, I want to have it arrive at my catch-all 404 page over HTTPS.

Here’s my 000-default.conf file (I’m not using an .htaccess file):

<VirtualHost *:80>
	ServerName example.com
	Redirect permanent / https://example.com
</VirtualHost>

<VirtualHost *:443>
	ServerName example.com

	ServerAdmin webmaster@localhost
	DocumentRoot /var/www/html

	SSLEngine on
	SSLCertificateFile <location here>
	SSLCertificateKeyFile <location here>

	SSLCACertificateFile <location here>

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

	ErrorDocument 404 /404.html
	ErrorDocument 400 /400.html
</VirtualHost>

<Directory /var/www/>
	Options Indexes FollowSymLinks MultiViews
	AllowOverride All
	Order allow,deny
	allow from all
</Directory>

I’m a newbie at this so there’s probably an embarassing mistake in here

I greatly appreciate any help!

Show comments

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.

This question was answered by @bcanseco:

Thanks a lot! I’ve heard of Let’s Encrypt before - I’m definitely going with them once this cert expires. Anyway, luckily I fiddled with this some more and found a simple solution. For anyone looking at this in the future:

<VirtualHost *:80>
   ServerName example.com/ <--- add a slash here
   Redirect permanent / https://example.com/ <--- and here
</VirtualHost>

Makes a lot of sense, really stupid that I overlooked this.

View the original comment