Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.htaccess redirect both http:// with and without www and https://www to https:// non-www

I'm looking to redirect all of:

http://
http://www.
https://www.

to https://

This may have been answered here before, but having gone through the questions that seem relevant they are all not quite what I'm after, and everything I've tried resulted in a redirect loop or not working as hoped.

For example:

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www.example.com
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]

RewriteCond %{HTTPS_HOST} ^example.com
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]

Thanks in advance for your help.

like image 833
PaulSkinner Avatar asked Oct 06 '22 11:10

PaulSkinner


1 Answers

Try:

RewriteCond %{HTTP_HOST} !^energenie4u\.co\.uk$ [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://energenie4u.co.uk/$1 [R=301,L]
like image 70
Jon Lin Avatar answered Oct 10 '22 01:10

Jon Lin