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 |1 Answer
Reset to default 2Yes, 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
版权声明:本文标题:404 error - Multiple 404 Pages 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736669015a1946814.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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