I expanded on the excellent answer by @Pheonix.
I am using Apache with virtualhosts, so this specifically applies to that.
I had four variations of URLs - www.thisdomain.com, thisdomain.com, www.thisdomain.org and thisdomain.org. I selected thisdomain.org as the definitive one. I split the virtualhost entry in Apache so I had one directory for the com and one for the org. I left all the PHP code alone in the org, and had an empty directory for the com. In the com directory I created a .htaccess file like @Pheonix suggested. I expanded it a little so that www.thisdomain.com and thisdomain.com would be redirected to thisdomain.org.
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{http_host} ^thisdomain.com [NC]
RewriteRule ^(.*)$ http://thisdomain.org/$1 [R=301,NC,L]
RewriteCond %{http_host} ^www.thisdomain.com [NC]
RewriteRule ^(.*)$ http://thisdomain.org/$1 [R=301,NC,L]
I could probably simply this by using [OR] and combining the two RewriteCond conditions and having only one RewriteRule.
Then in the directory for the org, I added another .htaccess file like this:
RewriteEngine On
RewriteCond %{http_host} ^www.thisdomain.org [NC]
RewriteRule ^(.*)$ http://thisdomain.org/$1 [R=301,NC,L]`
This redirects www.thisdomain.org to thisdomain.org. And now all four URL variations go to thisdomain.org and there is no duplicate content.