Change the Sitemap URL Base in Rank Math

The default Rank Math sitemap URL is https://example.com/sitemap_index.xml. The rank_math/sitemap/base_url filter adds a folder in front of it, so the index and every child sitemap move to a path like /search-sitemaps/sitemap_index.xml.

You might want this to keep sitemaps out of the site root, to avoid a clash with another tool that expects sitemap_index.xml, or to match the URL structure of a site you migrated to Rank Math.

To control what goes into the sitemap instead, see exclude a post type from the Rank Math sitemap.

The Code

Add the filter to your child theme’s functions.php file or to a custom plugin. This example moves the sitemaps under search-sitemaps/:

/**
 * Filter to change the Sitemap URL Base
 *
 * @param string $base New URL Base
 */
add_filter( 'rank_math/sitemap/base_url', function( $base ){
	return 'search-sitemaps/';
}, 10, 1 );

After saving the file, go to Settings > Permalinks and click Save Changes. That flushes the rewrite rules, and until you do it the new URL returns a 404.

How It Works

Rank Math builds every sitemap URL as home_url( $base . $file ). By default $base is an empty string, or index.php/ if your permalink structure includes index.php.

The filter is documented in the Rank Math filters and hooks reference.

Its value is used for the rewrite rules that serve the sitemaps and for the links Rank Math prints: the entries inside the sitemap index, the Sitemap: line in the virtual robots.txt, and the URL shown in the plugin settings.

Two details trip people up:

  • Keep the trailing slash. Rank Math appends the file name directly, so search-sitemaps without a slash gives you search-sitemapssitemap_index.xml.
  • Don’t add a leading slash. The same string goes into the rewrite rule, and WordPress matches rules against the path without a leading slash, so the sitemap would 404.

How to Find Your Rank Math Sitemap URL

The exact address, including any custom base, is shown at the top of Rank Math SEO > Sitemap Settings > General, next to “Your sitemap index can be found here”. Rank Math also redirects /sitemap.xml to /sitemap_index.xml at the site root.

If your site still uses plain permalinks (?p=123), there are no pretty sitemap URLs. The index is served at ?sitemap=1 instead.

To get the URL in PHP, use Rank Math’s template tag. Rank Math doesn’t register a sitemap object on rank_math(), so rank_math()->sitemap returns null.

if ( function_exists( 'rank_math_get_sitemap_url' ) ) {
	$sitemap_url = rank_math_get_sitemap_url(); // Includes the filtered base.
}

After You Change the Base

Moving the sitemap breaks any place that has the old URL stored. Go through this list:

  • Submit the new index URL in Google Search Console and remove the old one. Our guide to creating and submitting a sitemap walks through it.
  • If robots.txt has a hard-coded Sitemap: line, update it by hand. Rank Math skips adding its own line when one already exists.
  • On Nginx, Rank Math shows rewrite rules on the Sitemap Settings screen. They include the base, so copy the updated rules into your server config.
  • The built-in /sitemap.xml redirect still points to /sitemap_index.xml at the root, so don’t rely on it once the base changes.

FAQs

Common questions about the Rank Math sitemap URL:

What is the default Rank Math sitemap URL?
It's /sitemap_index.xml on your home URL, for example https://example.com/sitemap_index.xml. Each post type and taxonomy gets its own child sitemap, such as post-sitemap.xml and category-sitemap.xml.
Can I rename sitemap_index.xml itself?
Yes, with the rank_math/sitemap/index/slug filter. Return the new name without .xml, then save your permalinks. The base filter changes the folder, the slug filter changes the file name.
What does the rank_math/sitemap/index filter do?
It doesn't change the URL. It appends raw XML to the end of the sitemap index, so you can list extra <sitemap> entries, such as a sitemap generated by another plugin.
Why does my new sitemap URL return a 404?
The rewrite rules haven't been flushed, or the base is missing its trailing slash or has a leading one. Save Settings > Permalinks once, and on Nginx update the server rewrite rules as well.
Discussion
[  0 Comments  ]

Leave a Comment

To add code, use the buttons below. For instance, click the PHP button to insert PHP code within the shortcode. If you notice any typos, please let us know!