Permanent 301 redirect of ANY forum link from http://domain.any to http://www.domain.any

Most of you probably know that if somebody publish a link to your original domain without 'www', it will be probably displayed anyway. However it brings headache in the terms of SEO, since it could give you a "duplicated content" penalties from search crawler like Google.

(Despite I would disagree with this opinion...)

I have studied a lot of web pages regarding this subject, but most of them offer just a redirection using Apache's .htaccess. The code offered looks like:

Unfortunatelly I am not Apache guru and can not extend this code so it works correctly for all cases. What kind of cases? If you simply enter http://example.com/ it will work for you redirecting to http://www.example.com/. However if you enter http://example.com/somepage.html it will not redirect to that page properly (with 'www').

Here is the simple PHP code I've programmed to put on all miniBB pages. If you are using miniBB forums, you could use it at least for your forum script, forcing redirection of any kind of forum page which is accessed without 'www', to the original domain with 'www':