Coldfusion 301 redirect loop (www and non-www point to the same file)

495 Views Asked by At

I have searched for this everywhere but all I can find is related to .htaccess files. The server my website is hosted on is not using Apache so I can not use an .htaccess file.

I am trying to send people to the www version of my site whenever they type in the non-www domain. So if user types domain.com it will send them to www.domain.com. The problem is that they both are already pointing to the same coldfusion file.

I have a redirect set up that works but it is a 302 redirect:

<cfif cgi.server_name eq "mydomain.com">
    <cfset cgi.server_name="www.mydomain.com"/>
</cfif>

If i change the cfset to:

<cflocation statuscode="301" url="www.mydomain.com"/>

it creates an infinite redirect loop and I get an error page.

I need to use a 301 redirect so that Google will not double index my page and say that I have duplicate content as it sees the www and non-www as two seperate sites even though it points to same file. Also a 301 redirect will transfer link authority from the non-www to the www domain.

2

There are 2 best solutions below

1
On BEST ANSWER

To do this in CF you would do something combining your 2 I think.

<cfif cgi.server_name IS 'mydomain.com'>
     <cflocation statuscode="301" url="www.mydomain.com"/>
</cfif>

FYI - your code is in error for cflocaiton - cgi.server_name is not an attribute of cflocation.

As stated by others you can do this with your web server. Apache or IIS can both handle this task very easily. In IIS you set up a site with mydomain.com as your host header and set it up specifically as a redirect site to www.mydomain.com. good luck!

1
On

If you really want to do it with ColdFusion then try this in the onRequestStart() method of your Application.cfc file...

<cfif cgi.server_name eq "example.com">
    <cflocation statuscode="301" url="http://www.example.com" addToken="no">
</cfif>

But as others have said, you're better off redirecting at the web server layer.