2

I am using ColdFusion 9.0.1

I have a new site that is accessible through a few domains such as:

mydomain.com
www.mydomain.com
foo.mydomain.com

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".

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.

I will also make sure that I can access the site locally at 127.0.0.1

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.

Is this best place to put the code and does is this code complete? Is there a better way to code this?

<cfscript>
ThisHost = CGI.HTTP_HOST;
QString = CGI.QUERY_STRING;
GoToURL = "http://mydomain.com?" & QString;
if (ThisHost != "mydomain.com" && ThisHost != "127.0.0.1") {
    writeOutput("<cfheader statuscode='301' statustext='Moved permanently'>");
    writeOutput("<cfheader name='location' value='#GoToURL#'>");
    abort;
}
</cfscript>

UPDATE

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:

<cfscript
ThisHost = CGI.HTTP_HOST;
QString = CGI.QUERY_STRING;
GoToURL = "http://flyingpiston.com/?" & QString;
if (ThisHost != "flyingpiston.com" && ThisHost != "127.0.0.1:8500") {
    location(GoToURL, false, 301);  
}
<cfscript
Evik James
  • 10,335
  • 18
  • 71
  • 122
  • 2
    A better solution would be to do this at the web server level. – ale Dec 30 '11 at 17:24
  • Al, would I do that using .htaccess? What if I am using Hostek.com to host my site? Can I use .htaccess with IIS hosting companies? – Evik James Dec 30 '11 at 17:51
  • I did a lot of reading yesterday and determine that I can do URL rewrites using htaccess with Hostek. – Evik James Dec 31 '11 at 15:11

3 Answers3

2

I agree with other comments and answers that doing this at the web server is a better solution. I would also point out that if you want to use the script syntax, this is entirely wrong and will simply return a string to the browser:

writeOutput("<cfheader name='location' value='#GoToURL#'>");

In ColdFusion 9, you would instead use the location() function:

location("url", addtoken, statusCode);

In your case:

location(GoToURL, false, 301);

Your GoToURL variable is also missing the page name, so you'd need to add CGI.SCRIPT_NAME into the mix just before the ? to get the full URL being called.

With the tag syntax (as of ColdFusion 8 I believe), there is no need to use the CFHEADER tag for a 301 redirect. The CFLOCATION tag now supports a statuscode attribute which can be set to 301 as needed.

Justin Scott
  • 865
  • 4
  • 10
  • Yes, I agree that using the web server is a better solution and I will move in that direction. Until then, I did experiment and modify my code and came up with exactly what you suggested. I implemented it yesterday and it works great. Thanks... and happy new year!!! – Evik James Dec 31 '11 at 15:06
1

If you are on IIS 7.0 the you may be able configure your web.config file for a canonical redirect like so:

<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Redirect to WWW" stopProcessing="true">
          <match url=".*" />
          <conditions>
            <add input="{HTTP_HOST}" pattern="^domain.com$" />
          </conditions>
          <action type="Redirect" url="http://www.domain.com/{R:0}"
               redirectType="Permanent" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

Check out this link for additional options.

Micah
  • 1,221
  • 17
  • 42
  • Can I drop this into my web site or does it have to be done in IIS by the administrator? – Evik James Dec 30 '11 at 18:43
  • A typical default IIS installation may not have the [URL Rewrite Module](http://www.iis.net/download/urlrewrite) installed but many hosting providers do or will install it for you. If you want to install the module on your localhost or VPS then I'd recommend this resource: http://www.iis.net/download/urlrewrite. – Micah Dec 30 '11 at 19:18
1

The previous answer shows how to redirect domain.com to www.domain.com. If you want to redirect www.domain.com to 'domain.com', you will need a web.config file that looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <location path="" overrideMode="Inherit">
        <system.webServer>
            <rewrite>
                <rules>
                    <rule name="remove www" patternSyntax="Wildcard" stopProcessing="true">
                        <match url="*" />
                        <conditions logicalGrouping="MatchAny">
                            <add input="{HTTP_HOST}" pattern="www.*" />
                            <add input="{HTTP_HOST}" pattern="foo.*" />
                        </conditions>
                        <serverVariables />
                        <action type="Redirect" url="http://{C:1}" />
                    </rule>
                </rules>
            </rewrite>
        </system.webServer>
    </location>
</configuration>

The above web.config file was created on IIS 7.5 (Windows Server 2008 R2). Your host will need to install the URL Rewrite Module as mentioned above in order for this to work. The web.config file is stored in the root folder of your site. The above example will redirect 'www' and 'foo' sub-domains to the domain.

This 10 URL Rewriting Tips and Tricks article has been a good reference for me.

Scott Jibben
  • 2,229
  • 1
  • 14
  • 22