fitzilla fitzilla - 27 days ago 5
HTTP Question

http to https through .htaccess

I've had a look through existing questions, but I haven't really come across anything which works for me.

I'm currently running a site with a Secure SSL certificate. It can be accessed at a problem is the site can also be accessed at - I don't want this to be possible. I need it to redirect from http to https.

I found this one snippet of code to use in an .htaccess file.

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^ [NC]
RewriteRule ^(.*)$$1 [L,R=301]

This works fine when the user enters into their address bar, but I also need to add a conditional statement of some sort so that if the user enters '' or ''.

I've tried using the likes of [OR], but this ends up creating server errors.

Any help and suggestions is appreciated.



Try the following:

 RewriteEngine On
 RewriteCond %{HTTPS} !=on
 RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

Also, you can also redirect based on port number, for example:

 RewriteCond %{SERVER_PORT} ^80$
 RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

This will redirect all requests received on port 80 to HTTPS.