Discover how to dynamically redirect users from a custom 404 page to their requested URL after migrating to Umbraco. Learn the essential code and steps to implement a seamless solution.
---
This video is based on the question https://stackoverflow.com/q/150329/ asked by the user 'Gthompson83' ( https://stackoverflow.com/u/20483/ ) and on the answer https://stackoverflow.com/a/150460/ provided by the user 'DougN' ( https://stackoverflow.com/u/7442/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.
Visit these links for original content and any more details, such as alternate solutions, comments, revision history etc. For example, the original title of the Question was: 404 page that displays requested page
Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l...
The original Question post is licensed under the 'CC BY-SA 2.5' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 2.5' ( https://creativecommons.org/licenses/... ) license.
If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
Handling Dynamic URL Redirects on Your Custom 404 Page in Umbraco
Introduction: The 404 Page Dilemma
In the world of web development, encountering a 404 Not Found error is not uncommon, especially when migrating content from one Content Management System (CMS) to another—like moving to Umbraco. If you've made such a transition, you may have noticed that many of your website links have changed. While fixing these links might seem straightforward, there's a more significant concern: how to handle users who land on your custom 404 page without losing them to the void of broken links.
Imagine users trying to access bookmarked pages that no longer exist or have changed locations. Instead of letting them leave your site frustrated, you can redirect them seamlessly to the correct content. In this post, we’ll explore a practical solution to dynamically obtain the previously requested URL after a user is redirected to your custom 404 page.
The Challenge: Obtaining the Requested URL
Upon encountering a 404 error, getting the original URL that the user tried to visit can be tricky, especially when using IIS 6.0. You might instinctively reach for the HTTP_REFERER variable, but it often returns null or doesn't provide the desired results. Instead, what you need is the complete original URL passed within the query string when an error occurs.
The Solution: Redirecting Users from a Custom 404 Page
Here’s a structured way to implement a dynamic redirect using C# in an ASP.NET application after setting up your custom 404 error handling page.
Step 1: Grabbing the Original URL
When an error occurs, IIS appends the original URL to the query string. Using the Request.RawUrl, you can extract this URL. Below is a breakdown of how to accomplish this:
Extract the Original URL:
[[See Video to Reveal this Text or Code Snippet]]
Here, targetPage will contain the original request, which you can modify based on your needs.
Handle Different Query Formats:
Depending on how the error path is formatted, you’ll want to check if it starts with a ? and adjust accordingly:
[[See Video to Reveal this Text or Code Snippet]]
Step 2: Redirecting to the New Page
Once you have the original URL, you may want to redirect it to the new equivalent page in Umbraco. Below is a continuation of the previous code to handle this:
Identify Old ASP Pages:
[[See Video to Reveal this Text or Code Snippet]]
Redirect Specific Cases:
You could then handle specific patterns in your URLs, like changes in key sections:
[[See Video to Reveal this Text or Code Snippet]]
Conclusion: A Seamless User Experience
By implementing this dynamic redirect method on your custom 404 page, you can ensure that users are not left feeling lost after a link fails to work. Instead, they will be gracefully guided to the correct page, maintaining their experience while improving site usability.
Adapt and modify the logic to fit your specific URL patterns, and you'll turn problematic 404 errors into a smooth navigation experience. That way, your users can seamlessly find the content they were looking for, enhancing their journey through your website.
If you are facing challenges with broken links post-migration or want to learn more about managing redirects effectively, implement this solution today and enhance user satisfaction on your site!