-2

I know many people have ask similar not to say the same question before:

How can I redirect http to https (Using Apache)?/Why does it not do so?

There is even a "code" snippet on the Wikipedia page of HTTP 301 status code that redirects to https.

But after hours of searching I tried many different methods out of with only one is working:

<meta http-equiv="refresh" content="https://example.org">

as a default page at /var/www/html/index.html

Even though it successfully redirects it is more than ugly and also not really liked by search engines and increases loading time.

To get us all on one level this is my Apache configuration. I also use Django in combination with Apache in case that has any relevance.

So here is a listing of all my attempts:

  1. Use another VirtualHost

    <VirtualHost *:80>
        Redirect permanent / https://example.org/
    </VirtualHost>
    
  2. Use a rewrite that redirect everything that not connects on port 443 to the corresponding https page:

    RewriteEngine On
    RewriteCond %{SERVER_PORT} !443
    RewriteRule ^(/(.*))?$ https://%{HTTP_HOST}/$1 [R=301,L]`
    
  3. Use the implementation mentioned from Wikipedia:

    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
    RewriteCond %{HTTPS} on
    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
    RewriteEngine On
    RewriteCond %{SERVER_PORT} 80
    RewriteRule ^(.*)$ https://example.org/$1 [R,L]
    

I tried these options (where it made sense) in both apache2.conf and .htaccess files. Obviously with no success. Weirdly enough when I visit example.org and then after it has loaded manually type example.org/# the last two methods worked. I have tried also with only example.org/ and the same procedure but without #. But that does not work.

What other options are there (for a 301-Redirect)? Why does this not work? And if it possible: How can I modify it to get it working?

Thomas
  • 4,155
  • 5
  • 21
  • 28
Pixdigit
  • 11
  • 4
  • if you just do a simple search of this site you would find lots of answers to your question. – user9517 Jan 07 '17 at 22:46
  • The redirect in your attemp 1 should work. Don't use rewrite rules for such a task. Did you restart your apache server to read the new configuration? – Thomas Jan 08 '17 at 12:04
  • @Hanginoninquietdesperation Obviously I have researched quite a lot and tried out several methods. If you know of any method other than I mentioned your help is welcome. In addition I would not have asked that question (_because_ there were so many similar ones) if I had not yet tried the solutions in those questions. – Pixdigit Jan 08 '17 at 12:16
  • @Thomas thanks for your answer since those errors could be made easily. I went even as far as restarting the whole machine or purposely make a wrong config only to check if the configuration file was loaded. But why is rewrite a bad choice? Many resources (even Wikipedia) recommend this way of configuring a redirect to https. – Pixdigit Jan 08 '17 at 12:19
  • Well, the [apache docs](http://httpd.apache.org/docs/2.4/rewrite/avoid.html) state "...`mod_rewrite` should be considered a last resort...". – Thomas Jan 08 '17 at 12:24

1 Answers1

0

the first redirect should work, if it doesn't don't think too hard, then you are simply not "landing" in that virtualhost/server/config, if you have more *:80 virtualhosts make sure they all have the ServerName directive with their own name, otherwise Apache won't know where to deliver the requests. To make sure you have configured your virtualhosts correctly you can type:

apachectl -S

...and make sure no virtualhosts have the same name (or none), otherwise the first match wins.

Also, try to connect locally, try against the ip directly, and use a non-caching browser to make sure you are seeing the correct response and not cached responses, or your name resolution is pointing to other server. As in:

curl -I -H"Host:myserver.example.com" http://192.168.1.10/

Briefly In virtualhost this is enough:

Redirect / https://myserver.example.com/
ezra-s
  • 2,215
  • 1
  • 7
  • 13
  • 1
    As you were pointing out: There was indeed another virtualhost. Since I use Apache only for ssl negotiation I was not _too_ bothered with configuration and did not see that in sites-enabled there was a default configuration which probably caused the problem. – Pixdigit Jan 09 '17 at 20:35