Using the URL Rewriting module could be the way to go. Try using configuration similar to the following:
<system.webServer>
<rewrite>
<rules>
<clear />
<rule name="Redirect to WWW" enabled="true" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern=".*" />
</conditions>
<action type="Redirect" url="http://www.domain.com/{R:0}" />
</rule>
</rules>
</rewrite>
</system.webServer>
This will redirect without showing an error page. If you want to show an error page then you'll need to redirect to a page that takes the return URL and does a client-side redirect to the right place.
As @inspile says you're going to have trouble doing it for sub-pages of the site. You may be able to do it using the referer to make sure it's from a link on the main site. Again the URL Rewrite module is the way to go here.
Cheers