Forcing a unique domain name + https with an htaccess file in R301

Why make a 301 redirection when switching to HTTPS?

You may know that changing your domain name requires setting up a 301 redirection on your site. It’s like indicating your change of address, similar to what you would do with postal mail when moving to a new home…
With a 301 redirection, you inform search engines about this domain name change, ensuring you don’t lose your rankings.

When your site switches to HTTPS, the issue is the same. Your address changes, so it’s essential to set up a 301 redirection, or else you risk losing your ranking on Google.

What code to add to the htaccess file to force HTTPS and a unique domain name?

Here’s the code to add at the top of your htaccess file to set up the redirection on your site.
This code forces the domain name on lines 1 and 2, then forces HTTPS on lines 4 and 5.