Home > Software design >  Self Hosted Website www redirect to non www
Self Hosted Website www redirect to non www

Time:12-08

I am self hosting a website on a Synology NAS. I have set up an SSL certificate, set up the DNS records on Google Domains. If I enter the website, andrewr.ca, into a browser it works fine. If I enter enter image description here

And my Google Domains account looks like this. [enter image description

enter image description here

My SSL cert looks like this:

enter image description here

Could someone please point me in the right direction here. I just want to be able to enter www.andrewr.ca and it works the exact same as if I enter andrewr.ca

I have tried changing the CNAME, changing the .htaccess and also adding a different SSL certificate but had no luck with anything I try so far.

CodePudding user response:

This honestly probably has two separated answers out there for you to find, but I am posting this just because it's a solution we use currently in our setup to rewrite non https to https AND non www to www in one swoop. These are the rewrite directives in my production .htaccess

#SSL  = 1
RewriteCond %{HTTP_HOST} !^example.com$ [NC]
RewriteRule ^/?(.*)$ https://example.com/$1 [R=301,L]


# Redirect www and http to https - non-www
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(. )$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]

This is not for everyone and ONLY works if you have a single domain in your setup -- Which the top two lines of the htacces file there are put there to deal with multiple domains on the single files set to canonicalize the domain .. Once you have multiple domains in there .. You WILL NEED a separated SSL for each domain, AND the www for it to work .. But as for a SINGLE domain, this will forward to https - non www without the need for a secondary SSL on the www

  • Related