Redirect to HTTPS with ServerAlias

From InToSSH's Tutorials & Scripts
Jump to navigation Jump to search

If you have VirtuaHost that has both the ServerName example.com and ServerAlias www.example.com when you use LetsEncrypt and you want both of those URLs to redirect to HTTPS, the Rewrite will look like this

RewriteEngine on
RewriteCond %{SERVER_NAME} =example.com [OR]
RewriteCond %{SERVER_NAME} =www.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]