• 孙锡源
    管理员

    没太看明白你的意思

  • 孙锡源
    管理员

    指的是哪里的自动补全?这句话没主语,着实不知道指的啥

  • 孙锡源
    管理员

    不建议禁用该功能,这个功能其实是用来改进SEO的,见WordPress内核源码注释:

    https://core.trac.wordpress.org/browser/tags/3.5.1/wp-includes/canonical.php#L13

    为了防止你打不开这个地址,我将内容贴在这里:

    /**
     * Redirects incoming links to the proper URL based on the site url.
     *
     * Search engines consider www.somedomain.com and somedomain.com to be two
     * different URLs when they both go to the same location. This SEO enhancement
     * prevents penalty for duplicate content by redirecting all incoming links to
     * one or the other.
     *
     * Prevents redirection for feeds, trackbacks, searches, and
     * admin URLs. Does not redirect on non-pretty-permalink-supporting IIS 7+,
     * page/post previews, WP admin, Trackbacks, robots.txt, favicon.ico, searches,
     * or on POST requests.
     *
     * Will also attempt to find the correct link when a user enters a URL that does
     * not exist based on exact WordPress query. Will instead try to parse the URL
     * or query in an attempt to figure the correct page to go to.

    如果你执意要禁用,可以使用此插件:https://litepress.cn/plugins/disable-url-autocorrect-guessing

  • 孙锡源
    管理员

    URL自动补全的内部执行逻辑是怎样的?

    翻了下源码,实际的查询是由以下语句控制的:

    $strict_guess = apply_filters( 'strict_redirect_guess_404_permalink', false );
    
    if ( $strict_guess ) {
        $where = $wpdb->prepare( 'post_name = %s', get_query_var( 'name' ) );
    } else {
        $where = $wpdb->prepare( 'post_name LIKE %s', $wpdb->esc_like( get_query_var( 'name' ) ) . '%' );
    }

    你可以发现,他分两种模式,如果你不通过过滤器修改查询模式的话,它默认执行的是一个LIKE模糊查询。也就是说如果你输入了一个l,那么它会匹配所有slug包含l这个字母的文章和页面。之后会返回匹配到的第一个结果。至于第一个结果是如何计算的,那个是MySQL的算法范畴了,深挖下去的话无穷无尽……

    如何人为对结果干预?

    你可以通过下面这个过滤器在WordPress内置的查询方法执行之前接管整个过程:

    /**
     * Short-circuits the redirect URL guessing for 404 requests.
     *
     * Returning a non-null value from the filter will effectively short-circuit
     * the URL guessing, returning the passed value instead.
     *
     * @since 5.5.0
     *
     * @param null|string|false $pre Whether to short-circuit guessing the redirect for a 404.
     *                               Default null to continue with the URL guessing.
     */
    $pre = apply_filters( 'pre_redirect_guess_404_permalink', null );

    关闭URL补全的另一种方法

    之前的使用插件关闭是在Google上检索的,这会翻源码的时候发现可以直接通过一个过滤器关闭:

    /**
     * Filters whether to attempt to guess a redirect URL for a 404 request.
     *
     * Returning a false value from the filter will disable the URL guessing
     * and return early without performing a redirect.
     *
     * @since 5.5.0
     *
     * @param bool $do_redirect_guess Whether to attempt to guess a redirect URL
     *                                for a 404 request. Default true.
     */
    if ( false === apply_filters( 'do_redirect_guess_404_permalink', true ) ) {
        return false;
    }

     

  • 随意之光
    楼主

    非常感谢您的帮助,因为我曾经对页面的父页面进行修改,所以完全禁用确实会导致网站出现404,我在您提供的资料中发现了strict_redirect_guess_404_permalink

     

    add_filter( 'strict_redirect_guess_404_permalink', '__return_true' );
    

     

    将其设置为完全匹配更符合我的想法,不会出现驴唇不对马嘴的猜想,并且在修改父页面时也可进行跳转

    最后再次感谢您的帮助

  • 正在查看 5 条回复
    • 哎呀,回复话题必需登录。

    加入 LitePress 论坛 ,参与知识分享与交流
    登录 注册 进行评论
    立即加入