0

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

a.p.
  • 3,248
  • 7
  • 30
  • 48
  • Does this answer your question? [How to redirect all HTTP requests to HTTPS](https://stackoverflow.com/questions/4083221/how-to-redirect-all-http-requests-to-https) – Don't Panic Mar 03 '21 at 13:08
  • Unfortunately it does not help. I am not an Apache or Unix expert so any step by step help would be appreciated. At the moment I have removed the 'Redirect permanent' statement from the .conf file so that users do not get a Page Not found error (instead they see the http version instead of the https one) – a.p. Mar 04 '21 at 09:17
  • 1
    There are several answers there which absolutely, positively work :-) I'd suggest simply copying one of them - removing whatever redirect-related config you have first. If you have tried that and it didn't work, you need to edit your question and describe *exactly* what you tried, and *exactly* what happened. There are several different approaches listed in that question - while all of them should work, if one does not, try another. – Don't Panic Mar 04 '21 at 09:34
  • Thanks. I checked the link you suggested and tried several things which did not work. I finally checked out the official Apache documentation and found the correct way to do it. See me answer below. – a.p. Mar 05 '21 at 03:33

2 Answers2

0

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>
a.p.
  • 3,248
  • 7
  • 30
  • 48
0

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"
Ben791
  • 1