, ,

How to Redirect Specific 404 Errors to Friendly Pages

If you have a more or less dynamic website where you create and delete content (such as products that are no longer in stock, or temporary landing pages for specific campaigns), you are probably familiar with the importance of setting up redirects for URLs that no longer exist.

No one likes visitors encountering a 404 error when accessing those URLs: accumulating 404 errors not only offers a poor user experience but can also harm your seo ranking.

When faced with the problem of a deleted URL, logic tells us that we need to redirect that error to another related page.

But what happens if you need to delete obsolete content that you know will not be back online, but you don’t want to be constantly creating redirects?

In this article, I will show you a clean and professional solution that:

  • Automatically redirects a deleted custom post to a custom page
  • Does not affect other active posts at all
  • The normal 404 page still works, but not for our custom post
  • We offer better context to the user who encounters that error
  • We don’t have to create a redirect for every custom post we delete

Problem: when you don’t want to constantly create redirects, but a 404 error is not user-friendly

For this scenario, we do not want to have to create individual redirects every time a post from the group is deleted.

Nor can we create a mass redirect that captures all URLs containing the slug used by the content type we want to redirect, because it would also redirect active posts, preventing any visitor from accessing them. This is a critical error that we must avoid at all costs.

The key is to create a conditional redirect, which only activates when WordPress has already determined that the URL for the post type does not exist (404 error).


The Solution: A Differentiated Landing Page and a Custom PHP Snippet

The most elegant and precise way to solve this problem is by using a small PHP code snippet that we will add to our theme. This code acts after WordPress processes the request and detects that the page does not exist.

Example: Applying this logic to a discount coupon system

Imagine you have a post type in your WordPress installation to publish discount coupons or short-term promotions, each with its specific URL. This discount or promotion will be online for a few days and then deleted.

Since the number of incoming and outgoing promotions is high, you don’t want to be constantly creating redirects every time you delete a promotion, so you opt for the following:

  1. We create a landing page like “This promotion is no longer available” where all users visiting obsolete promotion URLs that we have deleted from our website will arrive.
  2. A PHP snippet analyzes every 404 error on the website, and if the page contains the slug of our post type, it redirects it to that landing page.

Advantages

The advantages of this system are several:

  • Improved workflow: we don’t have to manually redirect each promotion we delete.
  • Better user experience: the user lands on a page informing them that the promotion they are looking for does not exist, but that they can search for others.

Getting Started

Assuming you have created your landing page for deleted promotions, for example this one:

https://example.com/esta-promocion-ya-no-existe/

Now we will create the PHP snippet that will redirect the user to that page.

This is the code you need:


add_action( 'template_redirect', 'redirect_404_to_custom_page' );

function redirect_404_to_custom_page() {
    if ( ! is_404() ) {
        return;
    }

    $url_solicitada = $_SERVER['REQUEST_URI'];
    
    if ( preg_match( '#^/promocion/[^/]+/?$#', $url_solicitada ) ) {
        wp_redirect( 'https://example.com/esta-promocion-ya-no-existe/', 301 );
        exit;
    }
}

How does this code work?

  1. 404 Detection: The snippet first checks that we are dealing with a 404 error, not an existing page with a 200 code.
  2. Pattern Verification: It uses a regular expression to ensure that the requested URL matches the URL structure of your promotion system, meaning it contains the slug /promotion/.
    • The post type slugs for promotions in our system are always the same:
      • https://example.com/promocion/slug-promocion-1/
      • https://example.com/promocion/slug-promocion-2/
      • https://example.com/promocion/slug-promocion-3/
  3. Selective Redirection: Only if both conditions are met, it redirects the user to the custom page we have created, in our example, this one:
https://example.com/esta-promocion-ya-no-existe/

How to Implement This on Your Website?

Creating the landing page is straightforward; there’s no big secret. I recommend publishing it as noindex but allowing links to be visited, with these two meta tags:

noindex, follow

Any of the seo plugins for WordPress on the market allow you to do this easily.

As for the PHP snippet that will do the work, you have two options to add this code to your WordPress:

  1. Theme’s functions.php: You can add it directly to your active theme’s functions.php file. However, remember that if you update the theme, you will lose the code. To avoid this, you should use a child theme. If you don’t have a child theme or don’t know what it is, proceed to point 2.
  2. Snippet Plugin: Install a plugin like “Code Snippets” or “WP Code,” which allow you to add PHP code safely and organized, without needing to touch the theme files.

Important: Replace /promocion/ with the slug you use for your post type and also modify the URL of the custom page you have created.


Be Clear on Your Landing Page

The page to which you redirect your visitors when a promotion no longer exists should offer a good experience. I recommend including:

  • A clear message: explain that the promotion the user is looking for is no longer available, but do so in a friendly manner.
  • Add useful links: direct users to your current promotions page or to the homepage.
  • Search: Include a search bar so they can find similar promotions.

Advantages of this solution

  • Precise: only redirects deleted posts, never affects active ones.
  • Automatic: you don’t need to create redirects one by one for each deleted promotion.
  • SEO-friendly: The 301 redirect indicates to search engines that the content has been permanently moved and is no longer available, and it is not a classic 404 error.
  • Customizable: you can adapt the regular expression to any URL structure or post type you have (promotions, discounts, expired events…).

Conclusions

As you can see, managing deleted content in WordPress doesn’t have to be a headache. With this simple PHP snippet, you can offer a consistent and professional user experience, keeping your seo intact and without complicating your life with manual redirects.

The next time you delete an old event, remember that your visitors will not encounter a generic 404 error, but a page specifically designed to guide them to other content on your site.

Have you tried this solution?

Do you have any other strategies for managing deleted content?

Share your experience in the comments