Redirect to https using .htaccess

I have a problem with .htaccess

I have a certificate on my server, and it is only for https://example.com (not https://www.example.com ), so I'm trying to redirect .htaccess 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 works. At the moment I have this, but it does not seem to work for

http://www.example.com/subfolderI 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
+4
source share
2 answers

Replace your current code with this

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]
+1
source

This code does not work for me, I found that works for me

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
0

All Articles