简体   繁体   English

在Wordpress中禁用404页面

[英]Disable 404 Page in Wordpress

I want user to type any URL, example: 我希望用户键入任何URL,例如:

mywebsite.com/helloworld

After, with PHP i'd like to take slug helloworld , and print it on the page. 之后,使用PHP我想使用slug helloworld ,并将其打印在页面上。

By default Wordpress shows 404 page. 默认情况下,Wordpress显示404页。 My idea was to disable this page, but not sure if there's a better solution. 我的想法是禁用此页面,但不确定是否有更好的解决方案。

How i can achieve that? 我该如何实现?

add_filter( 'pre_handle_404', function() {
    # you can do anything you want here but the easiest and safest is
    # wp_redirect( 'your url with query parameters from the failing 404 url' );
    # exit();
    return FALSE;
} );

404 is one of many HTTP Status Codes and as such if you completely remove the page, you'll need to both disable it in WordPress and on your web server, eg Apache or Nginx. 404是许多HTTP状态代码之一 ,因此,如果您完全删除该页面,则需要同时在WordPress和Web服务器(例如Apache或Nginx)上将其禁用。 This is not advised as it will make browsers display legitimate 404's with uncontrollable/undesirable responses. 建议这样做,因为它会使浏览器显示合法的404,并且具有无法控制/不希望的响应。

If your question is "the default/my templates 404 page is terrible and I want to change it" refer to the Codex page on custom 404 page or use one of the numerous plugins designed to template error pages. 如果您的问题是“默认/我的404模板页面很糟糕,我想更改它”,请参考自定义404页面上Codex页面,或使用众多旨在为错误页面模板的插件之一。

I would also, personally, advise against directing error pages to a page of your choosing, eg your homepage. 我个人也建议不要将错误页面定向到您选择的页面,例如您的主页。 This does very little except confuse people wondering why example.com/helloworld goes to example.com. 除了使人们感到困惑的是为什么example.com/helloworld转到example.com之外,这几乎没有什么作用。 From a programmers perspective, it could add time to your development as you may find it harder to diagnose why you're getting redirected to your homepage rather than it coming up with 404 or 500 which each tell you different information. 从程序员的角度来看,它可能会增加开发时间,因为您可能会发现很难诊断为什么将您重定向到主页,而不是用它来的404500来告诉您不同的信息。

To go off of @magenta you can add conditions to this as well. 要取消使用@magenta,也可以为此添加条件。 This is what I did: 这是我所做的:

add_action( 'pre_handle_404', function() {
    global $wp_query;

    if(!$wp_query->found_posts){
        if($wp_query->query['post_type']=="clients"){
            wp_redirect( '/login' );
            exit;
        }
    }

    return FALSE;
} );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM