【问题标题】:Redirects - htcaccess or cloudflare?重定向 - htaccess 或 cloudflare?
【发布时间】:2020-09-25 14:31:01
【问题描述】:

我的客户网站有问题。

问题是似乎在一个不存在/没有内容的 URL 上进行了重定向:

www.henparty-houses.com/liverpool

但是,如果我们将结束 URL 更改为另一个没有内容 / 不存在的 URL,我们将获得正确的页面加载(即页面不存在)。

www.henparty-houses.com/leeds

===

/liverpool URL 上似乎有某种重定向。

问题是在创建页面时,以 URL /liverpool 结尾 - 无法使用此 URL,因为 wordpress 认为它​​已经在使用/被占用。

我可能遗漏了一些明显的或某种错误,但我需要以某种方式清除 /liverpool URL 以便可以使用内容,并确保在使用 /liverpool 创建包含内容的页面时没有重定向!

有人可以帮忙吗?

---- 检查此重定向时,这是输出。我检查了 clouflare 并且看不到任何重定向设置:

结果: www.henparty-houses.com/liverpool

HTTP/1.1 301 永久移动 日期:格林威治标准时间 2020 年 9 月 25 日星期五 10:08:36 内容类型:文本/html;字符集=iso-8859-1 传输编码:分块 连接:保持活动 设置 Cookie:__cfduid=d440b3cca7954a48bdc3f84b50e73ea7a1601028515; expires=周日,20 年 10 月 25 日 10:08:35 GMT;路径=/;域名=.henparty-houses.com;仅http; SameSite=松懈 地点:https://www.henparty-houses.com/liverpool 缓存控制:max-age=0 过期:格林威治标准时间 2020 年 9 月 25 日星期五 10:08:36 主机头:b7440e60b07ee7b8044761568fab26e8 X-代理缓存:MISS CF 缓存状态:动态 cf 请求 ID:05665580120000ed6fcaa52200000001 服务器:cloudflare CF-RAY:5d83f1e01a84ed6f-SJC

https://www.henparty-houses.com/liverpool

HTTP/2 302 日期:格林威治标准时间 2020 年 9 月 25 日星期五 10:08:39 内容类型:文本/html;字符集=UTF-8 设置cookie:__cfduid=d974040f170c5090b9a4e61af9590d1b61601028518; expires=周日,20 年 10 月 25 日 10:08:38 GMT;路径=/;域名=.henparty-houses.com;仅http; SameSite=松懈 x-cache-enabled:真 p3p: CP="ALL DSP NID CURa ADMa DEVa HISa OTPa OUR NOR NAV DEM" x-redirect-by: WordPress 地点:https://www.henparty-houses.com 缓存控制:max-age=0 到期:格林威治标准时间 2020 年 9 月 25 日星期五 10:08:38 主机头:b7440e60b07ee7b8044761568fab26e8 x-proxy-cache: 小姐 cf-缓存状态:动态 cf 请求 ID:05665588c800009623ca33e200000001 期望-ct: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct" 服务器:cloudflare cf 射线:5d83f1ee09ee9623-SJC

https://www.henparty-houses.com

HTTP/2 200 日期:格林威治标准时间 2020 年 9 月 25 日星期五 10:08:41 内容类型:文本/html;字符集=UTF-8 设置cookie:__cfduid=d63a3cce54fa1ecc1e3d8bbf7c7415b8d1601028519; expires=周日,20 年 10 月 25 日 10:08:39 GMT;路径=/;域名=.henparty-houses.com;仅http; SameSite=松懈 x-cache-enabled:真 p3p: CP="ALL DSP NID CURa ADMa DEVa HISa OTPa OUR NOR NAV DEM" 关联: ; rel="https://api.w.org/", ;相对=“替代”;类型=“应用程序/json”,; rel=短链接 变化:接受编码 缓存控制:max-age=0 到期:格林威治标准时间 2020 年 9 月 25 日星期五 10:08:40 主机头:b7440e60b07ee7b8044761568fab26e8 x-proxy-cache: 小姐 cf-缓存状态:动态 cf 请求 ID:0566558f4e0000930a5d2f8200000001 期望-ct: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct" 服务器:cloudflare cf-射线:5d83f1f87ede930a-SJC

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    我认为您不会在 .htaccess 文件中找到答案。从标题中可以看出,重定向是由 WordPress 本身发出的。

    x-redirect-by: WordPress
    

    如果你去你的 WordPress 安装目录,你可以使用下面的命令找到设置这个 header 的函数。

    grep --line-number --with-filename --fixed-strings -i -R 'x-redirect-by' .
    

    结果很短:

    ./wp-includes/pluggable.php:1284:                * Filters the X-Redirect-By header.
    ./wp-includes/pluggable.php:1296:                       header( "X-Redirect-By: $x_redirect_by" );
    

    不出所料,设置此标头的函数称为wp_redirect

    简而言之,问题可能不是服务器配置。我认为正在发生的事情是您的一个页面的 slug 设置为 liverpool

    通常,当 WordPress 收到对不存在页面的请求(触发 404 错误)时,它首先尝试将请求重定向到“规范” URL(请参阅 this post)。这些规范的 URL 被称为“slugs”。

    如果您的现有页面之一将 slug 设置为 liverpool,这将触发 URL 规范化过程。因此,该页面可能被设置为隐藏,或者是手动重定向用户,因为它需要用户登录,或无数其他事情。无论哪种方式,手动重定向都是 302 重定向的原因。找出到底发生了什么的最好方法是找到带有特定 slug 的页面。

    我倾向于相信该页面现在存在的原因是302 表示Found。如果一个页面之前使用了 liverpool 的 slug 然后对其进行了更改,则正确的响应应该是 301 Moved Permanently 并重定向到页面的当前版本。我的想法是,存在一个页面,其中包含有目的地将客户端重定向到主页的利物浦蛞蝓。因此,解决方案很可能是向下跟踪一页。

    您可以通过直接访问数据库而不是通过用户界面来显着加快此过程。 wp_posts 表包含大部分页面内容(请参阅the WordPress documentation 以获得更深入的解释),wp_postmeta 表包含帖子元数据。

    我将从运行这样的查询开始(我假设您使用的是 MariaDB 或 MySQL,但 Postgres 或任何查询将是类似的,如果不完全相同的话)。

    SELECT `wp_posts`.`post_name` FROM `wp_posts` WHERE `wp_posts`.`post_name` LIKE '%liverpool%';
    

    这个查询应该让你接近。如果在那里找不到任何东西,您可能需要检查wp_postmeta 表。根据网站数据库的大小,在 UI 中逐页浏览并手动检查实际上可能更容易,因为很多条目要么是序列化对象、HTML,要么通常很难在视觉上解析。

    SELECT `wp_postmeta`.* FROM `wp_postmeta`;
    

    显然,您可以在此处使用另一个 LIKE 子句,这可能有助于在运行此子句之前先尝试。请记住,它可能需要一段时间才能运行,因为它必须根据通配符匹配过滤结果,因此首先运行此基本查询可能会有所帮助,以便您获得所有结果,然后是另一个控制台上的特定查询,因此您可以在执行其他查询时尝试查看输出。我不确定你使用的硬件有多快,但我是根据个人经验说的,哈哈。

    我将首先对您正在使用的任何表运行以下查询,以便您可以获取表结构的句柄。

    DESCRIBE `wp_postmeta`;
    DESCRIBE `wp_posts`;
    

    您也可以通过SHOW TABLES 实际查看您正在使用的数据库中的所有表。

    您还应该看看this post。特别要注意它说您可以通过添加remove_action( 'template_redirect', 'wp_old_slug_redirect'); 来禁用重定向。我个人并不熟悉该功能,但如果您查看 wp_redirect 函数定义($WORDPRESS_INSTALL_DIR/wp-includes/pluggable.php,第 1246 行),您可以看到该函数应用了其他定义的过滤器。

    还请注意,该函数专门检查以确保状态代码在 300399 之间,这意味着 某事 正在从 @987654350 更改状态代码@到302

    这里是函数定义,为您提供方便和启发。

    /**
     * Redirects to another page.
     *
     * Note: wp_redirect() does not exit automatically, and should almost always be
     * followed by a call to `exit;`:
     *
     *     wp_redirect( $url );
     *     exit;
     *
     * Exiting can also be selectively manipulated by using wp_redirect() as a conditional
     * in conjunction with the {@see 'wp_redirect'} and {@see 'wp_redirect_location'} filters:
     *
     *     if ( wp_redirect( $url ) ) {
     *         exit;
     *     }
     *
     * @since 1.5.1
     * @since 5.1.0 The `$x_redirect_by` parameter was added.
     * @since 5.4.0 On invalid status codes, wp_die() is called.
     *
     * @global bool $is_IIS
     *
     * @param string $location      The path or URL to redirect to.
     * @param int    $status        Optional. HTTP response status code to use. Default '302' (Moved Temporarily).
     * @param string $x_redirect_by Optional. The application doing the redirect. Default 'WordPress'.
     * @return bool False if the redirect was cancelled, true otherwise.
     */
    function wp_redirect( $location, $status = 302, $x_redirect_by = 'WordPress' ) {
        global $is_IIS;
    
        /**
         * Filters the redirect location.
         *
         * @since 2.1.0
         *
         * @param string $location The path or URL to redirect to.
         * @param int    $status   The HTTP response status code to use.
         */
        $location = apply_filters( 'wp_redirect', $location, $status );
    
        /**
         * Filters the redirect HTTP response status code to use.
         *
         * @since 2.3.0
         *
         * @param int    $status   The HTTP response status code to use.
         * @param string $location The path or URL to redirect to.
         */
        $status = apply_filters( 'wp_redirect_status', $status, $location );
    
        if ( ! $location ) {
            return false;
        }
    
        if ( $status < 300 || 399 < $status ) {
            wp_die( __( 'HTTP redirect status code must be a redirection code, 3xx.' ) );
        }
    
        $location = wp_sanitize_redirect( $location );
    
        if ( ! $is_IIS && 'cgi-fcgi' !== PHP_SAPI ) {
            status_header( $status ); // This causes problems on IIS and some FastCGI setups.
        }
    
        /**
         * Filters the X-Redirect-By header.
         *
         * Allows applications to identify themselves when they're doing a redirect.
         *
         * @since 5.1.0
         *
         * @param string $x_redirect_by The application doing the redirect.
         * @param int    $status        Status code to use.
         * @param string $location      The path to redirect to.
         */
        $x_redirect_by = apply_filters( 'x_redirect_by', $x_redirect_by, $status, $location );
        if ( is_string( $x_redirect_by ) ) {
            header( "X-Redirect-By: $x_redirect_by" );
        }
    
        header( "Location: $location", true, $status );
    
        return true;
    }
    

    【讨论】:

    • 谢谢。有了这个,我设法解决了它 - 它确实存在,但没有显示在 Wordpress 管理面板上。因此,使用数据库在 wp_posts 中的数据库中定位利物浦,然后删除该条目即可解决。非常感谢您的详细回答
    • 很高兴,我很高兴它有帮助
    猜你喜欢
    • 2017-02-16
    • 1970-01-01
    • 2019-08-11
    • 2016-04-25
    • 1970-01-01
    • 1970-01-01
    • 2018-12-29
    • 1970-01-01
    • 2015-02-22
    相关资源
    最近更新 更多