There is no code required to do this. You can deal with this via the web.config.
<system.webserver>
<rule name="Remove WWW" patternsyntax="Wildcard" stopprocessing="true">
<match url="*" />
<conditions>
<add input="{CACHE_URL}" pattern="*://www.*" />
</conditions>
<action type="Redirect" url="{C:1}://{C:2}" redirecttype="Permanent" />
</rule>
</system.webserver>