'Redirect http to https on apache not working for http links that include a filename
I have recently setup SSL on my apache server (the server is hosted on DigitalOcean). I have followed the instructions to setup the certificate and edit to the server conf file to redirect http to https. Everything works file but I have a problem when using an http url that includes a file. In that case, the file is appended to the domain name without the / so the browser shows a File not found message (which is correct).
http://www.my-website.com redirects correctly to https://www.my-website.com
however,
http://www.my-website.com/file1.html redirects to https://www.my-website.comfile1.html (the / after the server name is missing)
Can someone tell me what the problem is? The redirection commands in the apache .conf file are as follows:
<VirtualHost *:80>
ServerName my-website.com
Redirect permanent / https://www.my-website.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
</VirtualHost>
Thanks
Solution 1:[1]
Following the link suggested in the comment by Don't Panic, I tried the solution from the original Apache documentation, http://httpd.apache.org/docs/current/rewrite/avoid.html#redirect, and it worked. I am not sure if the problem was the lack of the quotes in the redirect statement (I previously tried the exact same thing without the quotes and it did not work). The solution that worked for me is the following:
<VirtualHost *:80>
ServerName www.example.com
Redirect "/" "https://www.example.com/"
</VirtualHost>
Solution 2:[2]
The syntax for the Redirect directive from the Apache documentation is:
Redirect [status] [URL-path] URL
The issue in this case is that Redirect appends any remaining path after the URL-path parameter to the URL.
Hence in "http://www.my-website.com/file1.html", the remainder is "file1.html", which will be appended directly to the given URL, resulting in "https://www.my-website.comfile1.html".
The solution is to add a slash at the end of the URL, changing the line to:
Redirect permanent "/" "https://www.my-website.com/"
(Putting both parameters in quotes is also helpful, but not the cause of the issue).
Also, note that if the URL-path parameter was not "/", a trailing slash would not be needed on the URL, in effect the URL-path and URL need to match. For example:
Redirect permanent "/one" "https://www.my-website.com/two"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | a.p. |
Solution 2 | Ben791 |