WordPress at Your Fingertips

How to change WP Sitemap URL

In this example, consider the option to change the location (URL) of the WordPress Sitemap from wp-sitemap.xml to sitemap.xml.

add_action( 'init', 'add_new_url_main_sitemap' );
add_filter( 'home_url', 'fix_wp_sitemap_url', 11, 2 );

# Adds a rule with a new sitemap address
function add_new_url_main_sitemap() {
	add_rewrite_rule( '^sitemap\.xml$', 'index.php?sitemap=index', 'top' );
}

# Replaces the url from wp-sitemap.xml to sitemap.xml
function fix_wp_sitemap_url( $url, $path ) {

	if ( '/wp-sitemap.xml' === $path ) {
		return str_replace( '/wp-sitemap.xml', '/sitemap.xml', $url );
	}

	return $url;
}

After installing the code, you need to reset the Rewrite Rules (pretty permalinks) for the new rule to take effect. To do this, simply go to the "Settings → Permalinks" admin page.

No comments
    Log In