Redirecting example.squarespace.com to example.com

I recently switched to Squarespace for hosting my personal website. So far, it seems to be working pretty well. I did run into one problem that took a little while to solve:

I own the domain name markbowers.org, which is pointed to Squarespace's servers and configured as a custom domain in Squarespace's interface. However, I also have a Squarespace URL at markbowers.squarespace.com, that points to the exact same website.

  1. Users that visit the Squarespace URL be automatically redirected to my primary domain at markbowers.org.
  2. I need to retain access markbowers.squarespace.com while I am editing the webpage, so I need this rule to only be applied to users who are unauthenticated.
  3. If a user attempts to access markbowers.squarespace.com/tesla-coil, for example, I want to redirect them to markbowers.org/tesla-coil, not merely bounce them to markbowers.org.
  4. The script must do the above for both https and http requests to markbowers.squarespace.com.

To achieve this, I wrote the following Javascript:

<script>
  if (window.location.hostname == "markbowers.squarespace.com" 
      &&  Static.SQUARESPACE_CONTEXT.authenticatedAccount == undefined)
  {
    if (window.location.protocol == "http:")
    {
      window.location = window.location.href.replace("http://markbowers.squarespace.com","http://www.markbowers.org");   
    }
    if (window.location.protocol == "https:")
    {
      window.location = window.location.href.replace("https://markbowers.squarespace.com","http://www.markbowers.org");   
    }   
  }
</script>