18 Jan

Search Engine Friendly 301 Redirect in CFM Coldfusion

If you are finding that the www.sitename.com and sitename.com version of your site are both being indexed by the search engines you could hit an issue with the search engines seeing duplicate content – after all most sites have both site names resolving to the same underlying file structure. This can also be an issue for people with parked domains – if you’ve brought multiple domains with the common misspellings of your company name (for example), if the search engines index the parked domain you could have a duplicate content issue. Another issue is having links pointing to the various site names dilutes their value, but pointing them all at just one domain name you’ve got a better chance of higher rankings.

One of the best ways around this issue is to redirect all traffic destined for the domains you don’t want to get indexed to the site name you do want to get indexed. By using a 301 redirect the value of the links will also be redirected to the main site name.

Coldfusion CFM 301 Code

Place the following code into the header of any CFM document and it will redirect the page access to the correct site name. while preserving the script name and the query arguments.

<!-- If the server name is not www.sitename.com we can do the redirect to www.sitename.com.
  ' The only time we can is if the method is a GET
  ' (no way to pass along the POST arguments) and its on port 80 (don't want to redirect the SSL).
<cfif CompareNoCase(  CGI.SERVER_NAME , "sitename.com") is 0 AND
	CompareNoCase(  CGI.REQUEST_METHOD , "get") is 0 >
<cfheader statuscode="301" statustext="Moved permanently">
		<cfheader name="Location" value="http://www.sitename.com#CGI.SCRIPT_NAME#?#CGI.QUERY_STRING#">
		<cfheader name="Location" value="http://www.sitename.com#CGI.SCRIPT_NAME#">

11 thoughts on “Search Engine Friendly 301 Redirect in CFM Coldfusion

  1. I placed your 301 redirect code on our home page, just as you outline here. It works great. However, when doing a header check, the result comes back as a HTTP/1.1 302 Object Moved.

    This is NOT a 301 redirect then….its a 302.

    Anyone have any ideas or thoughts on this?

  2. The code
    <cfheader statuscode=”301″ statustext=”Moved permanently” >

    sets the return code to 301 – ensure that it is in correctly.

    Note if you’re cutting and pasting the code the quotes are being converted to smart quotes. Try hand coding.

  3. Very useful code, thanks – and it works fine across an entire site if you slot it into the application.cfm file.

    Just one question – any idea how to hide the default ‘index.cfm’ in the redirected URL? If you redirect all mysite.com pages to http://www.mysite.com, someone going to mysite.com will see http://www.mysite.com/index.cfm in the address bar – which means search engines will probably index http://www.mysite.com and http://www.mysite.com/index.cfm as separate pages, when they are infact the same. Any way around this?

  4. Use the header check at http://www.seoconsultants.com/tools/headers.asp on both URLs (with and without index.cfm) to check that both are being indexed and not redirected. That’s a tough one as most servers will report to the underlying code they’re at ‘index.cfm’ whether the URL in the address bar is that or not, so you can’t programmatically redirect easily.

    One way is to change the code above to detect you’re on the index.cfm page and not to append the page name to the redirect URL.

    Anyone have any suggestions?

  5. Thanks Dylan – fixed it by stripping out ‘index.cfm’ from CGI.SCRIPT_NAME where CGI.SCRIPT_NAME contains ‘index.cfm’ but NOT ‘index.cfm?’

    Much appreciated,

  6. I’m not able to get requests for http://www.domain.com/index.cfm to 301 redirect to http://www.domain.com.

    I’m trying to force the home page to ALWAYS be http://www.domain.com, because right now I can see that Google is indexing both and even assigning different PageRank to each, so I would like to transfer all the PageRank currently going to the index.cfm version of the home page over to the non-index.cfm version. This will ensure all PageRank accrues to one version.

    Any code for how to accomplish this?


  7. ColdFusion 8: Added the statusCode attribute.

    The HTTP status code, as follows:

    * 300 HTTP_MULTIPLE_CHOICES: The requested address refers to more than one entity.
    * 301 HTTP_MOVED_PERMANENTLY: The page is assigned a new URI. The change is permanent.
    * 302 HTTP_MOVED_TEMPORARILY: The page is assigned a new URI. The change is temporary.
    * 303 HTTP_SEE_OTHER: The client should try another network address.
    * 304 HTTP_NOT_MODIFIED: The requested resource has not been modified.
    * 305 HTTP_USE_PROXY: The requested resource must be accessed through the proxy given by the Location field.
    * 307 HTTP_TEMPORARY_REDIRECT: The requested data temporarily resides at a new location.

Leave a Reply

Your email address will not be published. Required fields are marked *