.htaccess rewrite condition to remove 'www' from 'https://www'

208 views Asked by At

I just need to add 'https' remove 'www' in every case:

I'm using following code:

    RewriteEngine on
    RewriteCond %{http_host} ^www\.example\.com [NC]
    RewriteRule ^(.*)$ http://example.com/$1 [R=301,NC]

    RewriteCond %{HTTPS} !on
    RewriteRule ^(.*)$ https://example.com%{REQUEST_URI} [R,L]

For whatever reason it works for the first two cases, but I can't make it work for the last one.

I couldn't even solve the last case isolated trying it e.g. like:

RewriteEngine on
RewriteCond %{https_host} ^www\.example\.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,NC]

Any help very appreciated!

1

There are 1 answers

4
Louis Loudog Trottier On BEST ANSWER

i'm sure this is a duplicate but here the code i use:

RewriteEngine on

#This removes the www.
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [L,R=301,NC,QSA]

#This makes redirect to https
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]