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 On
RewriteCond %{HTTPS} off
RewriteRule ^ 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:
sudo a2enmod rewrite
sudo a2enmod ssl
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>
sudo service apache2 restart
Using mod_rewrite is not the recommended way instead use virtual host and redirect.
In case, if you are inclined to do using mod_rewrite:
RewriteEngine On
# This will enable the Rewrite capabilities
RewriteCond %{HTTPS} !=on
# This checks to make sure the connection is not already HTTPS
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
# This rule will redirect users from their original location, to the same
location but using HTTPS.
# i.e. http://www.example.com/foo/ to https://www.example.com/foo/
# The leading slash is made optional so that this will work either in
# httpd.conf or .htaccess context
Reference: Httpd Wiki - RewriteHTTPToHTTPS
If you are looking for a 301 Permanent Redirect, then redirect flag should be as,
R=301
so the RewriteRule will be like,
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]
Actually, your topic is belongs on https://serverfault.com/ but you can still try to check these .htaccess directives:
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*) https://%{HTTP_HOST}/$1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With