Is the canonical link not working properly?

110 Views Asked by At

I have added canonical links to all pages of my website. Let's take the home page as an example. Let's say https://sample.com is entered there. Here I used yoast wordpress plugin. enter image description here

When you go to the view page source on the home page of the website, it is shown as follows. enter image description here Despite this, when the PageSpeed Insights test is run in the Google search console, the following error is displayed in the SEO section. Document does not have a valid rel=canonicalIs not an absolute URL (//sample.com/) It is shown in the picture below. enter image description here The link_template.php file has the following code.

/**
 * Returns the canonical URL for a post.
 *
 * When the post is the same as the current requested page the function will handle the
 * pagination arguments too.
 *
 * @since 4.6.0
 *
 * @param int|WP_Post $post Optional. Post ID or object. Default is global `$post`.
 * @return string|false The canonical URL. False if the post does not exist
 *                      or has not been published yet.
 */
function wp_get_canonical_url( $post = null ) {
    $post = get_post( $post );

    if ( ! $post ) {
        return false;
    }

    if ( 'publish' !== $post->post_status ) {
        return false;
    }

    $canonical_url = get_permalink( $post );

    // If a canonical is being generated for the current page, make sure it has pagination if needed.
    if ( get_queried_object_id() === $post->ID ) {
        $page = get_query_var( 'page', 0 );
        if ( $page >= 2 ) {
            if ( ! get_option( 'permalink_structure' ) ) {
                $canonical_url = add_query_arg( 'page', $page, $canonical_url );
            } else {
                $canonical_url = trailingslashit( $canonical_url ) . user_trailingslashit( $page, 'single_paged' );
            }
        }

        $cpage = get_query_var( 'cpage', 0 );
        if ( $cpage ) {
            $canonical_url = get_comments_pagenum_link( $cpage );
        }
    }

    /**
     * Filters the canonical URL for a post.
     *
     * @since 4.6.0
     *
     * @param string  $canonical_url The post's canonical URL.
     * @param WP_Post $post          Post object.
     */
    return apply_filters( 'get_canonical_url', $canonical_url, $post );
}

/**
 * Outputs rel=canonical for singular queries.
 *
 * @since 2.9.0
 * @since 4.6.0 Adjusted to use `wp_get_canonical_url()`.
 */
function rel_canonical() {
    if ( ! is_singular() ) {
        return;
    }

    $id = get_queried_object_id();

    if ( 0 === $id ) {
        return;
    }

    $url = wp_get_canonical_url( $id );

    if ( ! empty( $url ) ) {
        echo '<link rel="canonical" href="' . esc_url( $url ) . '" />' . "\n";
    }
}

Is the canonical link showing like this because of an error in its generation?

1

There are 1 best solutions below

0
TipVisor On

The error was here. Automatic HTTPS Rewrites in cloudflare's SSL/TLS>Edge Certificates is turned off.enter image description here