admin管理员组

文章数量:1125586

Anyone know if it's possible to have multiple / different 404 pages for different purposes?

For example one 404 page for broken internal links (which presumably should be more apologetic) and a different one for mistyped URL (with more of a "try again" or "search our site" message)

Anyone know if it's possible to have multiple / different 404 pages for different purposes?

For example one 404 page for broken internal links (which presumably should be more apologetic) and a different one for mistyped URL (with more of a "try again" or "search our site" message)

Share Improve this question asked Jan 31, 2024 at 14:59 CWeinhoferCWeinhofer 212 bronze badges 2
  • do you have code that would tell the difference between the two? A theme has only 1 404 template but that doesn't mean you have to output HTML in that template, you could load a new template based on logic – Tom J Nowell Commented Jan 31, 2024 at 16:13
  • Do you have a sense of how to distinguish a mistyped URL than a broken internal url? Your web server logs ought to have 404 url instances and then in your 404.php file you can actually check what url was called and redirect. You can also use a Redirect plugin to account for frequently erroring urls. – artlung Commented Jan 31, 2024 at 22:52
Add a comment  | 

1 Answer 1

Reset to default 2

Yes, it's definitely possible to have multiple 404 pages for different purposes on a WordPress site, although it requires a bit of custom coding. You can achieve this by detecting the source of the 404 error and then redirecting to a specific 404 template based on the condition. Here’s a general approach to implement this:

Identify the Source of the 404 Error:

You need to determine the source of the 404 error. For instance, if it's a broken internal link or a mistyped URL. This could be done by checking the HTTP referer, which tells you the previous page the user was on. Be aware that the referer might not always be set. Create Custom 404 Templates:

Create different 404 templates in your theme for each type of error. For example, 404-internal.php for internal link errors and 404-external.php for mistyped URLs. Modify the 404.php Template:

Edit the 404.php file in your theme. Add a conditional check to determine the source of the 404 error. Based on the condition, include the appropriate custom 404 template. Here’s a basic example of what the code in your 404.php might look like:

<?php
$referer = $_SERVER['HTTP_REFERER'];

// Check if the referer is from your own domain
if (strpos($referer, home_url()) !== false) {
    // This means it's an internal link that caused the 404
    include('404-internal.php');
} else {
    // This means it's likely a mistyped URL or an external link
    include('404-external.php');
}
?>

Important Considerations:

The $_SERVER['HTTP_REFERER'] variable can be unreliable as it depends on the client's browser sending this information. Make sure to handle cases where the referer is not set. This solution assumes basic PHP and WordPress theme editing knowledge. If you're not comfortable with coding, you might want to consult with a developer. Always back up your site before making changes to theme files. By using this approach, you can provide a more tailored experience for users landing on your 404 pages, potentially improving user engagement and reducing bounce rates.

本文标签: 404 errorMultiple 404 Pages