0

Firstly, I know there is a million of copies of this question already but after hours of trying solutions from all of them I've got nowhere.

So I want to redirect all incoming HTTP traffic on my Apache web server to HTTPS. I've tried several approaches and most of them have had 1 of 2 results: a) a redirect loop or b) only the very top-level page is redirected (i.e no directories get sent to HTTPS).

My current virtualhost file looks like this:

<IfModule mod_ssl.c>  
    <VirtualHost *:80> 
    ServerName roboguy99.co.uk
    Redirect / https://roboguy99.co.uk/
    </VirtualHost>
    <VirtualHost *:443>
        ServerAdmin [email protected]
    ServerAlias www.roboguy99.co.uk
        ServerName roboguy99.co.uk
        DocumentRoot /var/www/
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
        SSLEngine on
        SSLCertificateFile /etc/apache2/ssl/apache.crt
        SSLCertificateKeyFile /etc/apache2/ssl/apache.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
        BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown
    </VirtualHost>
</IfModule>

This current config is causing a redirect loop.

Any help is greatly appreciated.

Possibly useful information:

  • Apache 2.2.22
  • Running on Debian (Raspberry Pi)
  • Accessing from Windows 8.1, Google Chrome

I'm away for the week so if I don't reply to any questions, that's why. I'm also going to remove the redirect while I'm away, so live-testing the site won't work at the moment.

6
  • Where did you get ServerAlias https:// www.roboguy99.co.uk? Remove the https:// from ServerAlias. Apr 12, 2015 at 22:25
  • I think it was a failed test from earlier. I've removed it, and I'm getting the same result. Apr 12, 2015 at 22:29
  • I see a few problems with your configuration, but none that would explain a redirect loop. Your IfModule block hasn't been closed. Your first vhost is missing a ServerName. The second vhost has a incorrectly formatted ServerAlias. What's missing from your question is which URLs are in the redirect loop? And do you have any .htaccess file, which could explain the problem?
    – kasperd
    Apr 12, 2015 at 22:29
  • I have a .htaccess file, although it's currently empty. The other things I will fix now, thanks. Do I need both this type of redirect and a RewriteRule in the .htaccess for it to work? EDIT: The IfModule is closed, I just missed it when copying the file :| Apr 12, 2015 at 22:30
  • Your site is behind CloudFlare? is a wordpress? Apr 13, 2015 at 0:30

2 Answers 2

1

Add those lines in VirtualHost *:80:

RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

Make sure you enabled mod_rewrite:

a2enmod rewrite
service apache2 restart

Check result with wget:

wget --spider domain.tld
2
  • I've tried this and my site no longer is accessible. On chrome it leads to example.com (a cached copy?) and on IE fails to load. Apr 18, 2015 at 8:54
  • Update: Tested on Chrome on another device, it's giving me a redirect loop. Apr 19, 2015 at 12:12
-1

Try to change your

Redirect / https://roboguy99.co.uk/

To

RewriteRule ^/(.*) https://roboguy99.co.uk/$1 [R,L]

On your first one you only redirect when user requests / with the one I propose to you you will redirect all and furturmore add whatever user requested after /

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .