Redirect http request -> https

April 25, 2013 5.6k views
Hi!! I activated 5$ plan vps yesterday, I'm quite impressed. I'm developing a management application, that have to run throught ssl. I created this virtualhost (following the guide on digital ocean): ServerAdmin ServerName ServerAlias DirectoryIndex index.html DocumentRoot /var/www/mps # CGI Directory ScriptAlias /cgi-bin/ /var/www/mps/cgi-bin/ Options +ExecCGI SSLEngine on SSLCertificateFile /etc/apache2/ssl/apache.crt SSLCertificateKeyFile /etc/apache2/ssl/apache.key and it works well, but I'll have issue in redirecting http traffic. I tried with: ServerName Redirect / but no luck. Ideas? Thank you in advance
1 Answer
The issue with the redirect is that you won't be able to preserve for the REQUEST_URI so if a user goes to they will end up on - as for why the redirect isn't working you may want to update it to:

Redirect permanent /

Alternatively you can use .htaccess + mod_rewrite to setup a rewrite rule as follows which would redirect requests and also preserve the requested path/document:

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

You can also replace HTTP_HOST with SERVER_HOST, HTTP_HOST refers to what the user provided in his URL when using his browser, while the SERVER_HOST refers to the Server Name that was defined in the config file for which the domain/IP matched in your configuration.
Have another answer? Share your knowledge.