Forcing https non-www for every variants on Ubuntu 18.10/Apache

March 30, 2019 167 views
Apache Ubuntu 18.04

Trying to force https non www on all variations of the url:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)(.+) [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)
RewriteRule ^ https://%2%{REQUEST_URI} [R=301,L]

This is the code that i’m using.

It works well for http w/o www but it doesn’t work on https://www.example.com/ (https with www)

P.S: i have two DNS records type A on my domain pointing to the same ip: @, www and a letsencrypt ssl certificate.

Be the first one to answer this question.