What role does the .htaccess file play in addressing duplicate content issues in PHP websites?

Duplicate content issues in PHP websites can arise when the same content is accessible through multiple URLs. This can negatively impact SEO rankings and confuse search engines on which version of the content to index. One way to address this issue is by using the .htaccess file to set up 301 redirects from duplicate URLs to the preferred canonical URL.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]