Apache rewrite rule to redirect non-www to www with Let’s Encrypt exception

By | February 7, 2020 | 0 Comment

If you would like to redirect your non-www url to www and also add Let’s Encrypt path (/.well-known/acme-challenge/) to exception, here are the rules

Add following to your non-ssl Virtual Host

RewriteCond %{REQUEST_URI} !^/\.well\-known/acme\-challenge/
RewriteRule (.*) https://www.vishalon.net%{REQUEST_URI} [R=301,L]

Add following to your ssl enabled Virtual Host

RewriteCond %{HTTP_HOST} !^www\.(.+)$ [NC]
RewriteRule ^(.*) https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

About Vishal Monpara

Vishal Monpara is a full stack Solution Developer/Architect with 13 years of experience primarily using Microsoft stack. He is currently working in Retail industry and moving 1's and 0's from geographically dispersed hard disks to geographically dispersed user leveraging geographically dispersed team members.