Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 https://www.example.co.uk a problem is the site can also be accessed at http://www.example.co.uk - 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} ^example.co.uk [NC]  RewriteRule ^(.*)$ https://example.co.uk/$1 [L,R=301] 

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

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

Any help and suggestions is appreciated.

Cheers.

like image 223
fitzilla Avatar asked May 07 '12 22:05

fitzilla


People also ask

How do I redirect HTTP to HTTPS in Linux?

There are several ways to redirect to HTTPS in Apache. If you have root access to the Linux server where Apache runs, the preferred way is to set up the redirection in the domain's virtual host configuration file. Otherwise, you can set up the redirection in the domain's . htaccess file.


1 Answers

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.

like image 83
BluesRockAddict Avatar answered Sep 17 '22 19:09

BluesRockAddict