Can I use the .htaccess file or another solution to address www and non-www duplicate content issues?

Ahrefs is returning a series of duplicate content URLs for my own website i’ve asked them about and which they’ve said needs to be addressed on the CMS side, and which they cannot help with.

The issues seem to stem from their Ahrefs Platform identifying these two URL structures:

https://mywebsite.com

and

https://www.mywebsite.com

This has been driving me crazy for ages now (on a different domain) – my question is, is there a way, by using something like the .htaccess file (or possibly on the webhost side), to address this issue?

The webhost (ionos) appears to have added the following line/s to the .htaccess file in the last month, which as I understand it addresses directing visitors from http: to https (unless I’m wrong):

RewriteEngine On

RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.mywebsite.com/$1 (R,L)

I’m also uncertain as to whether this is having an impact on the duplicate content question?

I’ll apologise in advance if this is covered elsewhere – I have trawled for the answer on stackexchange, but i cannot come to grips with (a) exactly what the problem is, and (b) how specifically to address it.