Ask Ben: Forward ColdFusion Requests To Another Domain

Posted August 21, 2007 at 6:02 PM by Ben Nadel

Tags: ColdFusion, Ask Ben

I just read your post on Exploring Path Info. I think it is pretty much exactly what I was looking for BUT I am not much of a programmer so I can't implement it. I have to change the domain of a site and need to 301 redirect all the URLs to the new domain... It is not a matter of just forwarding the old domain to the new - I need to forward literally every old URL to every new URL. This all has to do with maintain the search engine rankings in Google. All totaled there are well over 300,000 URLs that need to be redirected.

NOTE: Made one change to the code where query string was returning NULL.

There are a lot of variables that go into this. Things like URL Rewriting and 404 error handling can make this stuff much more complicated and need to be addressed individually. For this demo, I am going to assume that we are dealing with straight-up URLs and that no crazy URL magic is going on.

That being said, all we have to do for this is get the currently requested page URL and then swap out the old domain value for the new domain value using simple string manipulation. There are a few different ways to get the URL parts such as the ColdFusion CGI scope; however, I have been told that those are unreliable going from server to server, so for ease of use, I am going to use the URL String Buffer returned by the underlying ColdFusion Request object. Actually, this is doubly-nice since the StringBuffer already takes care of things like protocol, credentials, and port, so we don't even have to worry about HOW to string together all the URL parts.

Now, we just need a centralized place in the site so that all incoming ColdFusion URLs are redirected. NOTE that I say ColdFusion requests since Image and other File requests are not handled through the ColdFusion application server. I only know this aspect (the ColdFusion aspect). The Application.cfc or Application.cfm file serves as the easiest place to put this code:

  • <!--- Get the request object for the current page. --->
  • <cfset objRequest = GetPageContext().GetRequest() />
  •  
  • <!---
  • Get the requested URL string buffer (this will NOT
  • contain the query string).
  • --->
  • <cfset objURLBuffer = objRequest.GetRequestURL() />
  •  
  • <!--- Get the query string for the current request. --->
  • <cfset strQueryString = objRequest.GetQueryString() />
  •  
  •  
  • <!---
  • Append the query string to the URL buffer if one exists
  • for the current request. If no query string was present,
  • the GetQueryString() method call will return NULL.
  • --->
  • <cfif StructKeyExists( VARIABLES, "strQueryString" )>
  •  
  • <cfset objURLBuffer.Append( "?" & strQueryString ) />
  •  
  • </cfif>
  •  
  •  
  • <!---
  • ASSERT: At this point, our URL string buffer holds the
  • full URL of the page that is currently being requested.
  • Now that we have that, we need to forward users to
  • another domain.
  • --->
  •  
  •  
  • <!--- Convert the URL buffer to a string. --->
  • <cfset strURL = objURLBuffer.ToString() />
  •  
  • <!---
  • Instead of just blindly replacing domain values, make
  • sure that it exists (if for some reason, something goes
  • wrong, you might create an infinite loop of forwarding
  • (if the domain never changes).
  •  
  • CAUTION: I am using a regular expression here in case
  • you want to hanlde thinks like www or what not. If you
  • don't care about that, just use FindNoCase() and remove
  • the \b constructs.
  • --->
  • <cfif REFindNoCase( "\bOLD_DOMAIN\b", strURL )>
  •  
  • <!---
  • We found the OLD domain in the current URL. Now,
  • let's replace it with the NEW domain.
  •  
  • CAUTION: I am using regular expression here in case
  • you want to handle things differently. If you don't
  • care about RegEx, just use a ReplaceNoCase() instead.
  • --->
  • <cfset strNewURL = strURL.ReplaceFirst(
  • "\bswoop\b",
  • "NEW_DOMAIN"
  • ) />
  •  
  • <!--- Return the permanent redirect to new URL. --->
  • <cfheader
  • statuscode="301"
  • statustext="Moved Permanently"
  • />
  •  
  • <!--- Return new URL. --->
  • <cfheader
  • name="Location"
  • value="#strNewURL#"
  • />
  •  
  •  
  • <cfelse>
  •  
  •  
  • <!---
  • Something went wrong with our code and we are not
  • finding the old domain. Return a 500 error.
  • --->
  • <cfheader
  • statuscode="500"
  • statustext="Internal Server Error"
  • />
  •  
  • <!--- Clear the content buffer and return HTML. --->
  • <cfcontent
  • type="text/html"
  • reset="true"
  • />
  •  
  • <!---
  • Even though something went wrong, provide the user
  • with a fail-safe so that they can at least get to
  • the new domain.
  • --->
  • <p>
  • There was an error with the current page request.
  • Please access our new site
  • <a href="http://NEW_DOMAIN_HOME_PAGE">here</a>.
  • </p>
  •  
  • </cfif>
  •  
  • <!--- Abort out of page. --->
  • <cfabort />

You will notice that in my redirect code, I am not just blindly swapping domain values and returning the headers; first I am checking to see if the domain is found. Once you get your code up and running, this shouldn't be a problem, but, at least for testing, this will help prevent and infinite redirect loop from occurring.

Also, just incase something does go wrong, I am returning a 500 error and giving the user a last-chance link to get to the new site. Hope this helps a bit.




Reader Comments

Aug 22, 2007 at 7:59 AM // reply »
12 Comments

Alternative and in my opinion preferred way is to use mod_rewrite if using Apache. Couple fo lines in .htaccess will take care of whole thing (including images and other files)

RewriteEngine On

RewriteRule ^(.*)$ http://www.new.com/$1 [R=301,L]

IIS has something similar, I don't know exact solution for it.


Aug 22, 2007 at 9:02 AM // reply »
10,640 Comments

@Tero,

That looks pretty nice. Unfortunately, I don't have access to any rewriting filters where I host / test, so I can't go this route. In your example, maybe I am just not seeing it, but where are you matching the old domain name? It appears as if the current domain name isn't even part of the URL that is exposed to MOD Rewrite? Also, will $1 start with a forward slash?


Aug 22, 2007 at 9:50 AM // reply »
12 Comments

That would go to .htaccess in your webroot; only requests for that domain will be served there and thus matching is done earlier by Apache virtual hosts setup.

If your host allows you to use .htaccess and they have mod_rewrite enabled this would work for you - I would think that's quite common but it has been ages since I last used anything but dedicated servers one way or another.

I think you are right about forward slash, I just googled for example and copied what they had (http://www.tamingthebeast.net/articles3/changing-domain-names.htm)


Aug 23, 2007 at 1:13 PM // reply »
1 Comments

Ben, I'd be surprised if your current host does not allow .htaccess. I think that almost all hosts allow it. I know plenty of people who use it to help set up they blogs address string in wordpress. It has become a quite common feature with most webhosting.


Aug 23, 2007 at 1:21 PM // reply »
10,640 Comments

@Dave,

To be honest, I know nothing about htaccess files. In general, other than a 404 handler, I don't know much about anything that requires additional CFAdmin or IIS setup. I don't have access to that stuff, so I find ways of doing stuff without it.


Dec 11, 2007 at 8:13 AM // reply »
3 Comments

You're using a very commendable process to go about this. Scanning for the site first, etc. I can imagine the nightmare the original asker of the question must have gone through trying to do everything one by one like that. Surely there must be an easier way! You do have to assume, though, that no URL magic is going on - so, you still need to probably at least check one by one.


Sep 12, 2010 at 5:47 PM // reply »
6 Comments

Okay, I've tried every bit of code imaginable for 301 redirects. I've tried the cfheader way, looked into .htaccess and much more. For some reason non of these work for me. Currently Google is listing a page that is no longer available because I changed the name of the directory. So when someone clicks the page from Google is shows a page not found error. So would this be more of a page not found issue or could a 301 redirect work? I'm thinking more of a custom 404 template. If I place cfheader code on a page that currently exist, it creates a continuous loop. I must be completely lost or something. The domain never changed, I only changed some of the directory names and now use an index based file nameing convention for my pages. (Permalink sytle).
What to do what to do?


Sep 28, 2010 at 9:29 PM // reply »
10,640 Comments

@Neo,

Google should eventually change the page it has indexed if you keep returning it a 301 status "Moved Permanently". It should get the message that the page is *never* gonna be there again. If it is failing to do this, something must be going wrong.

I am confused as to the CFHeader you are putting on the page that currently exists. If that page is valid, what header are you trying to add?


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
InVision App - Prototyping Made Beautiful With Prototyping Tools Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Feb 10, 2012 at 7:21 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
Update! Instead of $(eval(options.insertAfter)).after(data['insertData']); I now use: var ajaxNode = document.createElement('span'); var parent = $(eval(options.insertAfter))[0].parentNode; ... read »
Feb 10, 2012 at 6:18 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
encountered this same, what I consider, jQuery bug last week. I'm building a site in which I load some content via AJAX. This content contains Linkedin share button placeholders which Linkedin API ne ... read »
Feb 10, 2012 at 11:30 AM
Cross-Origin Resource Sharing (CORS) AJAX Requests Between jQuery And Node.js
After you understand the concepts here, this is an awesome cheatsheet for enabling CORS in just about anything http://enable-cors.org/ ... read »
JM
Feb 10, 2012 at 9:10 AM
My Safari Browser SQLite Database Hello World Example
@Amy, Here is a very good tutorial on how to use JOIN: http://www.sqltutorial.org/sqljoin-innerjoin.aspx ... read »
Feb 10, 2012 at 4:42 AM
Building A Twitter-Inspired RESTful API Architecture In ColdFusion
This is great, very useful Ben. I spotted a small typo in the api.cgm listing: <cfthrow type="Unauthroized" /> Cheers Stefan ... read »
Feb 9, 2012 at 10:35 PM
CFDirectory Filtering Uses Pipe Character For Multiple Filters (Thanks Steve Withington)
I was wondering if there would be a filter you could apply so that you got everything but what you included in the filter. As in show me all docs that are not a .pdf. ... read »
Feb 9, 2012 at 10:29 PM
Learning ColdFusion 9: Application-Specific Data Sources
@Ben, No offence, but if people were really wanting advanced features they would be using a platform like ASP.NET MVC. CFML is so structurally compromised as a tag-based scripting language that ... read »
Feb 9, 2012 at 10:03 PM
Subversion - Cleanup Failed To Process The Following Paths
@Leviaguirre, do you still have problems with this? ... read »