Top SEM Tips

SEO and Link Building Tips

Search Engine Friendly 301 Redirect in CFM Coldfusion

with 11 comments

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
	CGI.SERVER_PORT is 80 AND
	CompareNoCase(  CGI.REQUEST_METHOD , "get") is 0 >
<cfheader statuscode="301" statustext="Moved permanently">
<cfif CGI.QUERY_STRING IS NOT "" >
		<cfheader name="Location" value="http://www.sitename.com#CGI.SCRIPT_NAME#?#CGI.QUERY_STRING#">
<cfelse>
		<cfheader name="Location" value="http://www.sitename.com#CGI.SCRIPT_NAME#">
	</cfif>
</cfif>

Written by Dylan

January 18th, 2006 at 3:00 am

11 Responses to 'Search Engine Friendly 301 Redirect in CFM Coldfusion'

Subscribe to comments with RSS or TrackBack to '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?

    Pete Radke

    30 May 08 at 11:09 am

  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.

    admin

    3 Jul 08 at 4:34 pm

  3. How can I use this script on the Application.cfm file, I want to run this function in the entire site. Let say redirect full domain A to domain B

    xomero

    7 Aug 08 at 5:02 pm

  4. It’s been a while since I worked on a Coldfusion site at the application level. The code above can be included on all files and will achieve the same effect – not the answer you were looking for but…

    Or have a look at http://www.sitepoint.com/forums/showthread.php?t=545129 and let me know what code works and I’ll post it here.

    Dylan

    12 Aug 08 at 6:29 pm

  5. 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?

    Spencer

    14 Aug 08 at 6:44 am

  6. 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?

    Dylan

    14 Aug 08 at 12:16 pm

  7. 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,
    Spencer

    Spencer

    15 Aug 08 at 8:29 am

  8. 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?

    Thanks

    Jane Galt

    10 Sep 08 at 10:47 am

  9. I don’t know an easy way to do this. The script will usually fill in CGI.SCRIPT_NAME with index.cfm whether accessed using / or /index.cfm

    Dylan

    10 Sep 08 at 11:51 am

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

    Karen

    26 Oct 08 at 4:52 pm

  11. Thanks, works great..!!!

    Willem-Alexander

    16 Jul 09 at 7:12 am

Leave a Reply