title: Is this the best place to use a 301 redirect to control which domain name is used?
body: <p>I am using ColdFusion 9.0.1</p>
  
  <p>I have a new site that is accessible through a few domains such as:</p>
  
  <pre><code>mydomain.com
  www.mydomain.com
  foo.mydomain.com
  </code></pre>
  
  <p>For SEO and tracking purposes, I want to make sure that only "mydomain.com" is indexed and accessed.  So, every request that tries to access my site through other domains will be 301 directed to "mydomain.com".</p>
  
  <p>I want to make sure that I capture and preserve the query string so that I don't just send people to the home page.</p>
  
  <p>I will also make sure that I can access the site locally at 127.0.0.1</p>
  
  <p>I wondering where in the code is the best place to do this SPECIFIC type of redirect.  My guess it's in application.cfc near the top, in the onRequestStart() method.</p>
  
  <p>Is this best place to put the code and does is this code complete? Is there a better way to code this?</p>
  
  <pre><code>&lt;cfscript&gt;
  ThisHost = CGI.HTTP_HOST;
  QString = CGI.QUERY_STRING;
  GoToURL = "http://mydomain.com?" &amp; QString;
  if (ThisHost != "mydomain.com" &amp;&amp; ThisHost != "127.0.0.1") {
      writeOutput("&lt;cfheader statuscode='301' statustext='Moved permanently'&gt;");
      writeOutput("&lt;cfheader name='location' value='#GoToURL#'&gt;");
      abort;
  }
  &lt;/cfscript&gt;
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I know this isn't the best way to accomplish what I need, because this task is much better suited to the web server's skill set.  Here's my code till I can implement this on the web server:</p>
  
  <pre><code>&lt;cfscript
  ThisHost = CGI.HTTP_HOST;
  QString = CGI.QUERY_STRING;
  GoToURL = "http://flyingpiston.com/?" &amp; QString;
  if (ThisHost != "flyingpiston.com" &amp;&amp; ThisHost != "127.0.0.1:8500") {
      location(GoToURL, false, 301);  
  }
  &lt;cfscript
  </code></pre>