In the fast-paced world of website management, changes are inevitable. Pages may move or be removed, and links can become outdated, leading to frustrating 404 errors for visitors and negatively impacting search engine rankings. That’s where fallback redirects come in. By using these redirects, you can ensure that visitors are always directed to the correct pages, even if the original URL is no longer valid. This creates a more ideal user experience, helps maintain search engine rankings, and keeps your Kentico website running smoothly. In this article, we’ll explore the benefits of custom fallback redirects and provide best practices for implementing them on your website.
Having a fallback redirect can be a lifesaver for websites with frequently changing or moving content. Imagine a well-maintained structure that guides visitors to their desired destination, with a news landing page to help them navigate to individual news nodes. But what happens when a news article is archived, and the page can’t be found? It would be helpful to have a fallback redirect. With this tool, you can ensure a flawless user experience by routing visitors back to the news landing page, even when content disappears. Using a fallback redirect will provide a better user experience that will keep visitors coming back for more.
If you’re managing a website with blogs that use a structure like site/blogs/(document-name), a site restructuring or migration can pose a challenge. For example, without a landing page for blogs on the new site, visitors may encounter 404 errors when trying to access a blog that has been moved or removed. By implementing a fallback routing for blogs, you can ensure that visitors will be seamlessly routed to the news landing page when they try to access a blog affected by the restructuring.
In addition to scenarios where content is frequently changing or moving, there are also cases where content needs to be removed from a website because it is time-sensitive, such as events that have passed or promotions that have ended. Automatic archiving can be a useful tool in this situation, as it can move content no longer relevant to a separate archive rather than deleting it altogether. However, even with automatic archiving in place, visitors may still try to access this content after it has been moved, resulting in 404 errors. Search engines may also be led astray by URLs that have been moved or deleted. By using a fallback redirect, you can ensure that visitors are routed to an appropriate page, such as an archived events page or a promotions archive, when they try to access content that has been removed due to time sensitivity. This will also preserve a consistent user experience and search engine rankings, even during a website cleanup.
Redirects play a critical role in website maintenance, ensuring a seamless user experience when content changes or moves. But how do you create these redirects, and where should they be stored? To maintain a clean and organized project, redirects should be added to a new class in a location for extensions. This allows for easy management and ensures a consistent naming convention. Then, when a 404 error occurs, the URL is checked against these rules to determine if a redirect should be triggered. It’s like having a virtual gatekeeper for your website, ensuring that visitors always find the right content. Proper implementation of redirects can help prevent frustration for visitors and ensure that your website remains user-friendly. Following best practices for creating and checking redirects, you can become an expert in website maintenance and optimization. Below is an example of a redirect extension to ensure that child pages are redirected to the landing page in the case of a 404 error.
public static class CustomRedirectExtension
{
/// Adds a redirect for Detail pages to redirect to the Landing
public static IApplicationBuilder UseCustomLandingRedirect( this IApplicationBuilder app )
{
if( app == null )
{
throw new ArgumentNullException( nameof( app ) );
}
return app.UseStatusCodePages(
async context =>
{
await Task.Run( ( ) =>
{
var originalPath = context.HttpContext.Request.Path;
var originalQueryString = context.HttpContext.Request.QueryString;
if( IsChildPage( originalPath ) )
{
context.HttpContext.Response.Redirect( GetLandingUrl( context ) );
}
} );
}
);
}
private static bool IsChildPage( PathString path )
=> path.StartsWithSegments( "/news", StringComparison.OrdinalIgnoreCase )
|| path.StartsWithSegments( "/blogs", StringComparison.OrdinalIgnoreCase );
private static string GetLandingUrl( StatusCodeContext context )
{
var landingNode = DocumentHelper
.GetDocuments()
.Published()
.TopN( 1 )
.FirstOrDefault();
var relativeUrl = DocumentURLProvider.GetUrlForCulture( landingNode, LocalizationContext.CurrentCulture.CultureCode );
var currentUrl = context.HttpContext.Request.GetDisplayUrl();
var uriBuilder = new UriBuilder( currentUrl );
uriBuilder.Path = relativeUrl.TrimStart( '~' );
return uriBuilder.ToString();
}
}
To use the custom redirect rules, they must be added to the Configure method in Startup.cs (or the app section, depending on the version of .NET). This is done by calling the appropriate method within the IApplicationBuilder, such as app.UseCustomLandingRedirects(). Including the code below in your startup configuration ensures that your website directs visitors to the right content, even when pages are moved or deleted. This helps to maintain a positive user experience on your website.
public void Configure( IApplicationBuilder app )
{
...
app. UseCustomLandingRedirects ();
...
}
Once the custom redirect rules have been implemented, site visitors and search engines will always be redirected to the correct pages, even if the original URL is no longer valid. It’s also possible to create new custom redirect classes and add them to the startup configuration to fall back to multiple URLs. In addition, by changing the page type retrieved, you can specify which page visitors should be directed to in the case of a 404 error. This allows for greater flexibility and control over your website’s content and navigation.
One of the great things about custom redirects is their flexibility. You can create as many fallback redirects as needed to ensure visitors always find what they’re looking for. Whether you’re handling news articles, blog posts, event pages, or other types of content, custom redirects ensure that visitors are always directed to the appropriate landing page. By taking a comprehensive approach to custom redirects and customizing them to meet your specific needs, you can create a highly effective website maintenance strategy that keeps your visitors engaged and satisfied. Additionally, by following the best practices outlined in this article and staying on top of changes to your website’s structure and content, you can ensure that your custom redirects continue to provide value and enhance the user experience over time.
By taking advantage of custom redirects, website owners and developers can ensure that visitors always find the content they’re looking for, even if a page has been moved, deleted, or archived. Creating custom redirects may require some effort upfront, but the benefits of user satisfaction and search engine optimization can be significant. In addition, by following the best practices outlined in this article, such as identifying the need for redirects, creating and checking redirect rules, and adding them to the startup configuration, you can become an expert in website maintenance and optimization. Ultimately, custom redirects are a powerful tool that can help you keep your website running smoothly and keep visitors engaged and satisfied.
Whether your team is looking to upgrade an existing site, tackle a complex integration, or start from scratch, our team of 35+ highly-skilled developers, designers, and digital strategists, including a Kentico MVP, has the expertise to take your brand from strategy to launch. As a Kentico Gold Partner for over 10 years, we bring a wealth of experience and proven success to every project we undertake. Send us a message to get started!
We love to make cool things with cool people. Have a project you’d like to collaborate on? Let’s chat!
Stay up to date on what BizStream is doing and keep in the loop on the latest in marketing & technology.