Welcome!

You will be redirected in 30 seconds or close now.

ColdFusion Authors: Yakov Fain, Jeremy Geelan, Maureen O'Gara, Nancy Y. Nee, Tad Anderson

Related Topics: ColdFusion

ColdFusion: Article

Handling 404 Errors for a Migrated Blog

The technology behind the solution

I just transitioned my blog in two huge ways: (a) I reassigned it to a different domain name, and (b) I changed the blogging engine I was using, which incidentally used a file organization structure that's incompatible with my new engine.

Furthermore, I decided to import all of my old posts into my new blog rather than leave the old and start anew.

I don't regret any of the changes I've made, because I've laid groundwork that I'll be much happier with in the future. But these changes can wreak havoc on my site's perceived status, since any links to my pages and any stale search engine referrals will now result in ugly 404 errors. Sure, we could set up a friendly 404 page that notifies the user of what's happened. However, why don't we eliminate the embarrassment of notifying the user at all and simply redirect them as best as possible?

An Example Scenario
In my case, I was moving my blog from the root of my site (www.nazin.com) to a sub-domain (http://blog.nazin.com). Furthermore, I was migrating from Blogger.com, which publishes an HTML page for each post, to WordPress, which loads everything from index.php and uses the post slug in the query string to determine what post to display. For instance, one highly trafficked page was www.nazin.com/2004/08/airtunes-concerns-answered.htm, and my new site housed this post under http://blog.nazin.com/index.php/airtunes-concerns-answered/. Once all the old HTML files were removed from the old site, those pages would come up as 404 Page Not Found errors.

The Technology Behind the Solution
To redirect the requests, assign a custom 404 page via the system administration for your site. This will naturally vary from one situation to the next depending on your server situation (Do you have direct server access? Do you have shared hosting?). Most Web servers will communicate the error and the address in the CGI query string. We'll use the original address the user attempted to navigate to as a means to redirect them to the post on the newly migrated blog. As always, I'll do this with CF.

Building a ColdFusion 404 Handler
Through the CGI structure, we can take a look at the input we're getting from the Web server for the error and address. Let's create a preliminary 404 handler page. Let's name it 404.cfm.

<cfoutput]#CGI.QUERY_STRING#[/cfoutput>

If we were to implement this and attempt to browse to the scenario page above, we'd get output similar to this:

404;http://www.nazin.com:80/2004/08/airtunes-concerns-answered.htm

To the human eye, it's immediately apparent how to take this output and create a redirect. At this point, it's simply a matter of text manipulation. Perhaps we might write some code like:

<cfscript>
    searchTerm=ListLast(CGI.QUERY_STRING,"/") ;
    searchTerm=ListDeleteAt(searchTerm,ListLen(searchTerm,"."),".") ;
    newURL="http://blog.nazin.com/index.php/" & searchTerm & "/" ;
    </cfscript>
    <cflocation addtoken="no" url="#newURL#">

Here, we're effectively telling ColdFusion:
(1) Grab just the filename of the address (producing "airtunes-concerns-answered.htm").
(2) Delete the filename's extension (producing "airtunes-concerns-answered"). There are UDFs that have been written to perform this function as well, such as ripExt() at cflib.org.
(3) Build the new URL and
(4) Redirect the browser to that location.

Try browsing to the deleted page, and the redirection works like a charm. However, there's a serious issue lingering in our solution: What if your post slugs don't always match from the old blog to the new blog, if you imported your old posts like I did?

Addressing Mismatched Slugs: A Different Approach
For my scenario page, the previous example worked perfectly. However, during the import process, WordPress didn't always create post slugs with exactly the same names as the HTML files generated by Blogger.com. Is it still possible to redirect the user?

Sure. Instead of attempting to redirect the user to the new post's address, try redirecting them to the more forgiving search page.

For the next example, let's use another high-traffic page that breaks the previous solution: www.nazin.com/2005/06/how-to-install-itunes-on-windows-xp.htm. On the new blog, this page is http://blog.nazin.com/index.php/how-to-install-itunes-on-windows-xp-sp2/; as you can see, the slug is slightly different because Blogger.com truncated it. But this isn't the only thing that could cause different slugs. Perhaps certain characters are treated differently in the different engines, or perhaps a customized slug was created. In any case, relying on the new blog's search engine will address the problem.

The code is actually simple:

<cfscript>
    searchTerm=ListLast(CGI.QUERY_STRING,"/") ;
    searchTerm=ListDeleteAt(searchTerm,ListLen(searchTerm,"."),".") ;
    searchTerm=Replace(searchTerm,"-","+","ALL") ;
    newURL="http://blog.nazin.com/?s=" & searchTerm ;
    </cfscript>
    <cflocation addtoken="no" url="#newURL#">

The only difference with this approach is that it converts all the dashes (-) in the filename to pluses (+), which are used in the URL string to indicate spaces. Thus, the URL generated by this code (http://blog.nazin.com/?s=how+to+install+itunes+on+windows+xp) would be the equivalent of someone searching for "how to install itunes on windows xp" in the search box.

The user will thus be redirected to the new blog, with a link directly to the page desired.

Further Enhancements
The final step you could take to really polish the process would be to modify the search results page on your blog to redirect the user to the search results page in the event that only a single record is returned. The code to do this would vary from one engine to the next, but it would achieve the same effect as the previous solution, even when your post slugs differ.

Migrating your blog to a new domain name or to a new blogging engine can be disruptive, especially when you have other sites or search engines linking to your old, out-dated URLs. With very little time and effort, however, a simple 404 handler that utilizes the techniques we just considered can take the pain out of the migration.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


@ThingsExpo Stories
DX World EXPO, LLC, a Lighthouse Point, Florida-based startup trade show producer and the creator of "DXWorldEXPO® - Digital Transformation Conference & Expo" has announced its executive management team. The team is headed by Levent Selamoglu, who has been named CEO. "Now is the time for a truly global DX event, to bring together the leading minds from the technology world in a conversation about Digital Transformation," he said in making the announcement.
"Space Monkey by Vivent Smart Home is a product that is a distributed cloud-based edge storage network. Vivent Smart Home, our parent company, is a smart home provider that places a lot of hard drives across homes in North America," explained JT Olds, Director of Engineering, and Brandon Crowfeather, Product Manager, at Vivint Smart Home, in this SYS-CON.tv interview at @ThingsExpo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA.
SYS-CON Events announced today that Conference Guru has been named “Media Sponsor” of the 22nd International Cloud Expo, which will take place on June 5-7, 2018, at the Javits Center in New York, NY. A valuable conference experience generates new contacts, sales leads, potential strategic partners and potential investors; helps gather competitive intelligence and even provides inspiration for new products and services. Conference Guru works with conference organizers to pass great deals to gre...
The Internet of Things will challenge the status quo of how IT and development organizations operate. Or will it? Certainly the fog layer of IoT requires special insights about data ontology, security and transactional integrity. But the developmental challenges are the same: People, Process and Platform. In his session at @ThingsExpo, Craig Sproule, CEO of Metavine, demonstrated how to move beyond today's coding paradigm and shared the must-have mindsets for removing complexity from the develop...
In his Opening Keynote at 21st Cloud Expo, John Considine, General Manager of IBM Cloud Infrastructure, led attendees through the exciting evolution of the cloud. He looked at this major disruption from the perspective of technology, business models, and what this means for enterprises of all sizes. John Considine is General Manager of Cloud Infrastructure Services at IBM. In that role he is responsible for leading IBM’s public cloud infrastructure including strategy, development, and offering m...
"Evatronix provides design services to companies that need to integrate the IoT technology in their products but they don't necessarily have the expertise, knowledge and design team to do so," explained Adam Morawiec, VP of Business Development at Evatronix, in this SYS-CON.tv interview at @ThingsExpo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA.
To get the most out of their data, successful companies are not focusing on queries and data lakes, they are actively integrating analytics into their operations with a data-first application development approach. Real-time adjustments to improve revenues, reduce costs, or mitigate risk rely on applications that minimize latency on a variety of data sources. In his session at @BigDataExpo, Jack Norris, Senior Vice President, Data and Applications at MapR Technologies, reviewed best practices to ...
Widespread fragmentation is stalling the growth of the IIoT and making it difficult for partners to work together. The number of software platforms, apps, hardware and connectivity standards is creating paralysis among businesses that are afraid of being locked into a solution. EdgeX Foundry is unifying the community around a common IoT edge framework and an ecosystem of interoperable components.
Large industrial manufacturing organizations are adopting the agile principles of cloud software companies. The industrial manufacturing development process has not scaled over time. Now that design CAD teams are geographically distributed, centralizing their work is key. With large multi-gigabyte projects, outdated tools have stifled industrial team agility, time-to-market milestones, and impacted P&L stakeholders.
"Akvelon is a software development company and we also provide consultancy services to folks who are looking to scale or accelerate their engineering roadmaps," explained Jeremiah Mothersell, Marketing Manager at Akvelon, in this SYS-CON.tv interview at 21st Cloud Expo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA.
"IBM is really all in on blockchain. We take a look at sort of the history of blockchain ledger technologies. It started out with bitcoin, Ethereum, and IBM evaluated these particular blockchain technologies and found they were anonymous and permissionless and that many companies were looking for permissioned blockchain," stated René Bostic, Technical VP of the IBM Cloud Unit in North America, in this SYS-CON.tv interview at 21st Cloud Expo, held Oct 31 – Nov 2, 2017, at the Santa Clara Conventi...
In his session at 21st Cloud Expo, Carl J. Levine, Senior Technical Evangelist for NS1, will objectively discuss how DNS is used to solve Digital Transformation challenges in large SaaS applications, CDNs, AdTech platforms, and other demanding use cases. Carl J. Levine is the Senior Technical Evangelist for NS1. A veteran of the Internet Infrastructure space, he has over a decade of experience with startups, networking protocols and Internet infrastructure, combined with the unique ability to it...
22nd International Cloud Expo, taking place June 5-7, 2018, at the Javits Center in New York City, NY, and co-located with the 1st DXWorld Expo will feature technical sessions from a rock star conference faculty and the leading industry players in the world. Cloud computing is now being embraced by a majority of enterprises of all sizes. Yesterday's debate about public vs. private has transformed into the reality of hybrid cloud: a recent survey shows that 74% of enterprises have a hybrid cloud ...
"Cloud Academy is an enterprise training platform for the cloud, specifically public clouds. We offer guided learning experiences on AWS, Azure, Google Cloud and all the surrounding methodologies and technologies that you need to know and your teams need to know in order to leverage the full benefits of the cloud," explained Alex Brower, VP of Marketing at Cloud Academy, in this SYS-CON.tv interview at 21st Cloud Expo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clar...
Gemini is Yahoo’s native and search advertising platform. To ensure the quality of a complex distributed system that spans multiple products and components and across various desktop websites and mobile app and web experiences – both Yahoo owned and operated and third-party syndication (supply), with complex interaction with more than a billion users and numerous advertisers globally (demand) – it becomes imperative to automate a set of end-to-end tests 24x7 to detect bugs and regression. In th...
"MobiDev is a software development company and we do complex, custom software development for everybody from entrepreneurs to large enterprises," explained Alan Winters, U.S. Head of Business Development at MobiDev, in this SYS-CON.tv interview at 21st Cloud Expo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA.
Coca-Cola’s Google powered digital signage system lays the groundwork for a more valuable connection between Coke and its customers. Digital signs pair software with high-resolution displays so that a message can be changed instantly based on what the operator wants to communicate or sell. In their Day 3 Keynote at 21st Cloud Expo, Greg Chambers, Global Group Director, Digital Innovation, Coca-Cola, and Vidya Nagarajan, a Senior Product Manager at Google, discussed how from store operations and ...
"There's plenty of bandwidth out there but it's never in the right place. So what Cedexis does is uses data to work out the best pathways to get data from the origin to the person who wants to get it," explained Simon Jones, Evangelist and Head of Marketing at Cedexis, in this SYS-CON.tv interview at 21st Cloud Expo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA.
SYS-CON Events announced today that CrowdReviews.com has been named “Media Sponsor” of SYS-CON's 22nd International Cloud Expo, which will take place on June 5–7, 2018, at the Javits Center in New York City, NY. CrowdReviews.com is a transparent online platform for determining which products and services are the best based on the opinion of the crowd. The crowd consists of Internet users that have experienced products and services first-hand and have an interest in letting other potential buye...
SYS-CON Events announced today that Telecom Reseller has been named “Media Sponsor” of SYS-CON's 22nd International Cloud Expo, which will take place on June 5-7, 2018, at the Javits Center in New York, NY. Telecom Reseller reports on Unified Communications, UCaaS, BPaaS for enterprise and SMBs. They report extensively on both customer premises based solutions such as IP-PBX as well as cloud based and hosted platforms.