Issue with Incorrect Homepage URL in Sitemap for Multilingual Site Using Transla

#867842
  • Resolved Antonio Polato
    Rank Math free

    Hello,

    I am experiencing an issue with the RankMath sitemap on my multilingual WordPress website that uses TranslatePress for language management. Here’s an overview of the problem:

    Website Details:
    Default Language: English
    Secondary Language: Italian
    TranslatePress is set to use /en/ for English and /it/ for Italian. The English version is the default, so the homepage is at https://www.website.it/en/.

    Issue:
    In the sitemap generated by RankMath SEO, the root URL https://www.website.it/ is being listed instead of the default language version https://www.website.it/en/. Since the root URL redirects to the English version, it should not be included in the sitemap.
    When I try to manually set the canonical URL for the homepage to https://www.website.it/en/, the homepage disappears from the sitemap entirely.

    Desired Outcome:
    I would like https://www.website.it/en/ to be listed as the homepage in the sitemap, not https://www.website.it/, while ensuring that all language-specific pages remain correctly listed under their respective /en/ and /it/ prefixes.
    Could you please assist me in resolving this issue so that the sitemap correctly reflects the default homepage URL?

    I appreciate your help and look forward to your response.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hello,

    We apologize for the issue you are facing with the sitemap on your multilingual WordPress website using Rank Math and TranslatePress. It seems that the incorrect homepage URL is being included in the sitemap, which can be frustrating.

    To resolve this issue, you can add a specific piece of code to your theme’s rank-math.php file. This code will help ensure that the correct homepage URL is listed in the sitemap. Here’s the code you should add:

    if ( 'page' === get_option( 'show_on_front' ) ) {
        add_filter( 'rank_math/sitemap/entry', 'compsupp_5514_filter_sitemap_home_page_entry', 10, 3 );
    }
    function compsupp_5514_filter_sitemap_home_page_entry( $url, $type, $post ) {
        global $sitepress;
        static $wpml_home_urls = null;
        static $frontpage_translations = null;
        static $default_frontpage_id = null;
        if ( ! $wpml_home_urls ) {
            $active_languages = $sitepress->get_active_languages();
            $wpml_home_urls   = [];
            foreach ( $active_languages as $code => $language ) {
                $wpml_home_urls[ $code ] = $sitepress->language_url( $code );
            }
            $default_frontpage_id   = get_option( 'page_on_front' );
            $frontpage_trid         = $sitepress->get_element_trid( $default_frontpage_id, 'post_page' );
            $frontpage_translations = array_map( WPMLFPObj::prop( 'element_id' ), $sitepress->get_element_translations( $frontpage_trid, 'post_page' ) );
        }
        if ( in_array( $post->ID, $frontpage_translations ) && $post->ID !== $default_frontpage_id  ) {
            $language_code = array_search( $post->ID, $frontpage_translations );
            $url['loc']    = $wpml_home_urls[ $language_code ];
        }
        return $url;
    }

    Note: We recommend taking a backup of your website’s file and database before making any changes to the code.

    This code should help ensure that the correct homepage URL is reflected in your sitemap. If you continue to experience issues after implementing this solution, please reply to this ticket, and a human agent from the Rank Math Support team will assist you further.

    Thank you for raising this concern, and we hope this helps resolve your issue!

    Sources:
    Multilingual SEO with Rank Math SEO & WPML » Rank Math

    Hello,

    Please follow these steps to remove the main site and add the /en/ to your page sitemap:

    Exclude your homepage using it’s ID from Rank Math Settings under Rank Math SEO > Sitemap Settings > Exclude Posts.

    There you need to enter the homepage ID to exclude it from the page sitemap

    screenshot

    Once done, add this filter on your site:

    add_filter( 'rank_math/sitemap/post_type_archive_link', function( $archive_url, $post_type ){
        if ( $post_type == 'page' ) {
            return home_url( '/en/' );
        }
        return $archive_url;
    }, 10, 2 );

    And here’s how you can add a filter/hook to your WordPress site:
    https://rankmath.com/kb/wordpress-hooks-actions-filters/

    After that, please follow these steps to flush your sitemap cache:

    1. Flush the Sitemap cache by following this video screencast:
    https://i.rankmath.com/pipRDp

    2. Exclude the Sitemap files of the Rank Math plugin in your caching plugin. The cache could be via a plugin or from the server. For plugins or Cloudflare, please follow this article:
    https://rankmath.com/kb/exclude-sitemaps-from-caching/

    3. Disable the transient cache for the sitemap by applying this filter. This will make sure that your XML sitemap always reflects the latest changes on your site:

    /** 
     * Filter if XML sitemap transient cache is enabled.
     *
     * @param boolean $unsigned Enable cache or not, defaults to true
     */
    add_filter( 'rank_math/sitemap/enable_caching', '__return_false');

    Hope that helps.

    Here’s an improved version of the text:

    **Subject: Issue with Duplicate “/en/” in Sitemap URLs**

    Hello, thank you for your response. I’ve noticed that the provided code seems to add an extra /en/ to the home URL, which already includes it. This results in the incorrect URL https://www.website.it/en/en/ appearing in the sitemap.xml.

    It seems that Rank Math identifies the homepage as https://www.website.it/en/ only when the snippet is active, but the snippet then appends an additional /en/. Without the snippet, the homepage remains set as https://www.website.it/ (without the /en/).

    Could you help resolve this issue so that the sitemap correctly displays https://www.website.it/en/?

    Thank you!

    Hello,

    We might need to take a closer look at the settings. Please edit the first post on this ticket and include your WordPress & FTP logins in the designated Sensitive Data section.

    Please do take a complete backup of your website before sharing the information with us.

    Sensitive Data Section

    It is completely secure and only our support staff has access to that section. If you want, you can use the below plugin to generate a temporary login URL to your website and share that with us instead:

    https://wordpress.org/plugins/temporary-login-without-password/

    You can use the above plugin in conjunction with the WP Security Audit Log to monitor what changes our staff might make on your website (if any):

    https://wordpress.org/plugins/wp-security-audit-log/

    Looking forward to helping you.

    Thank you.

    Hello,

    Since we did not hear back from you for 15 days, we are assuming that you found the solution. We are closing this support ticket.

    If you still need assistance or any other help, please feel free to open a new support ticket, and we will be more than happy to assist.

    Thank you.

Viewing 4 replies - 1 through 4 (of 4 total)

The ticket ‘Issue with Incorrect Homepage URL in Sitemap for Multilingual Site Using Transla’ is closed to new replies.