Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect to https using .htaccess

I have a problem with .htaccess

I have a certificate in my server, and it's only for https://example.com (not https://www.example.com) Therefore, I'm trying to do a .htaccess redirect to the correct url.

My intention is this:

http://www.example.com --> https://example.com
http://example.com --> https://example.com
https://www.example.com --> https://example.com

I tried different combinations and nothing seems to work. At the moment I have this, but seems like is not working for

http://www.example.com/subfolder, I don't know what is failing...

RewriteEngine On

# Follow symbolic links.
Options +FollowSymLinks

RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

# Prevent directory listings
Options All -Indexes
like image 559
Jumy Elerossë Avatar asked Oct 21 '22 03:10

Jumy Elerossë


2 Answers

Replace your current code by this one

Options -Indexes +FollowSymLinks
RewriteEngine On

# redirect "www" domain to https://example.com
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

# redirect http to https (at this point, domain is without "www")
RewriteCond %{HTTPS} =off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
like image 22
Justin Iurman Avatar answered Oct 24 '22 15:10

Justin Iurman


That code is not working for me, I found that, which works for me

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
like image 112
aislamfaisal Avatar answered Oct 24 '22 15:10

aislamfaisal