How to automatically redirect HTTP to HTTPS on Apache servers?
I have actually followed this example and it worked for me :)
NameVirtualHost *:80<VirtualHost *:80> ServerName mysite.example.com Redirect permanent / https://mysite.example.com/</VirtualHost><VirtualHost _default_:443> ServerName mysite.example.com DocumentRoot /usr/local/apache2/htdocs SSLEngine On # etc...</VirtualHost>
Then do:
/etc/init.d/httpd restart
RewriteEngine OnRewriteCond %{HTTPS} offRewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}
http://www.sslshopper.com/apache-redirect-http-to-https.html
or
http://www.cyberciti.biz/tips/howto-apache-force-https-secure-connections.html
Searched for apache redirect http to https
and landed here. This is what i did on ubuntu:
1) Enable modules
sudo a2enmod rewritesudo a2enmod ssl
2) Edit your site config
Edit file
/etc/apache2/sites-available/000-default.conf
Content should be:
<VirtualHost *:80> RewriteEngine On RewriteCond %{HTTPS} off RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}</VirtualHost><VirtualHost *:443> SSLEngine on SSLCertificateFile <path to your crt file> SSLCertificateKeyFile <path to your private key file> # Rest of your site config # ...</VirtualHost>
- Note that the SSL module requires certificate. you will need to specify existing one (if you bought one) or to generate a self-signed certificate by yourself.
3) Restart apache2
sudo service apache2 restart