How to Add Custom URLs and Sitemaps in Rank Math SEO
Content
Many WordPress users migrating from other SEO plugins or implementing advanced multilingual setups find that they need to add custom URLs to their Rank Math-generated sitemap. This is a common task for including PDF files, filter pages, or external sitemap indexes that aren't part of the standard WordPress content structure.
Why Custom URLs Are Not Included by Default
Rank Math SEO automatically generates sitemaps for standard WordPress content types (posts, pages, custom post types) that are registered within the database. Dynamically generated URLs, such as those created by filter parameters or files stored in specific directories, are not included because they are not recognized as standard content entities. This design ensures optimal performance and avoids sitemap bloat with potentially infinite dynamic URL combinations.
Common Solutions for Adding Custom URLs
1. Adding Individual URLs with a Filter
For adding specific, known URLs (like a set of PDFs), you can use the rank_math/sitemap/entry filter. The code snippet below demonstrates how to add all PDF files from a specific directory to the post sitemap. It's crucial to use the correct filter and ensure the file paths and URLs are accurately constructed.
// Add custom URLs to the sitemap
add_filter( 'rank_math/sitemap/entry', 'add_custom_urls_to_sitemap', 10, 2 );
function add_custom_urls_to_sitemap( $url, $type ) {
// Check if the type is post
if ( 'post' === $type ) {
// Define the custom folder where the PDF files are stored
$custom_folder = '/wp-content/uploads/custom-folder/';
// Get the list of PDF files in the custom folder
$pdf_files = glob( ABSPATH . $custom_folder . '*.pdf' );
// Loop through each PDF file
foreach ( $pdf_files as $pdf_file ) {
// Get the file name
$file_name = basename( $pdf_file );
// Get the file URL
$file_url = home_url( $custom_folder . $file_name );
// Add the file URL to the sitemap array
$url[] = array(
'loc' => $file_url,
'lastmod' => date( 'c', filemtime( $pdf_file ) ),
'changefreq' => 'monthly',
'priority' => 0.6,
);
}
}
// Return the modified sitemap array
return $url;
}
2. Adding an Entire External Sitemap Index
If you need to add a link to a completely separate sitemap file (e.g., one generated by another tool), you should use the rank_math/sitemap/index filter to add a new <sitemap> section to the main sitemap index. Note that the official Rank Math documentation recommends a specific method for this.
// Add an external sitemap to the sitemap index
add_filter( 'rank_math/sitemap/index', function( $xml ) {
$xml .= '<sitemap>
<loc>http://example.com/new-sitemap.xml</loc>
<lastmod>2020-09-14T20:34:15+00:00</lastmod>
</sitemap>';
return $xml;
}, 11 );
Important: For a more robust solution, it is highly recommended to follow the guide for creating custom sitemaps as referenced in the support threads.
3. Programmatically Clearing the Sitemap Cache
When content is added programmatically (e.g., via wp_insert_post), the sitemap may not update immediately due to caching. To force a refresh, you need to clear Rank Math's sitemap cache. The following code can be run after bulk programmatic operations.
// Clear the Rank Math sitemap cache
delete_transient( 'rank_math_sitemap_cache' );
Important Considerations and Limitations
- Multilingual Incompatibility: A recurring theme in the threads is limited compatibility with some multilingual plugins, notably Polylang. The Rank Math team has stated they have submitted compatibility code to the Polylang development team, but full integration is pending. This can result in only default language terms appearing in taxonomy sitemaps. Users experiencing this may need to seek a solution from the translation plugin's support or consider using a compatible alternative.
- Dynamic URLs: Rank Math does not add purely dynamic URLs, like those with filter parameters (
?filter=value), to the sitemap by default, as they are not stored posts in the database. The provided filter method is the primary workaround for this. - Caching Conflicts: If your sitemap URLs return a 404 error or do not display correctly, it is often due to server or plugin caching. Sitemaps must be excluded from caching. Review your caching plugin's settings (e.g., FlyingPress) to exclude all XML files and ensure your server's rewrite rules are correctly configured, especially on Nginx.
By using these filters and understanding the limitations, you can effectively extend your Rank Math sitemap to include the necessary URLs for optimal search engine indexing.
Related Support Threads Support
-
Add .pdf files in a folder to the sitemap filehttps://wordpress.org/support/topic/add-pdf-files-in-a-folder-to-the-sitemap-file/
-
Separate Multilingual Sitemapshttps://wordpress.org/support/topic/separate-multilingual-sitemaps/
-
Rank Math migration site map issuehttps://wordpress.org/support/topic/rank-math-migration-site-map-issue-2/
-
Sitemap when migrating form Yoasthttps://wordpress.org/support/topic/sitemap-when-migrating-form-yoast/
-
Polylang compatibilityhttps://wordpress.org/support/topic/polylang-compatibility-47/
-
Rank Math HTML site map order by Menu orderhttps://wordpress.org/support/topic/rank-math-html-site-map-order-by-menu-order/
-
Adding filter URLs to Sitemap by defaulthttps://wordpress.org/support/topic/adding-filter-urls-to-sitemap-by-default/
-
Customizing or Renaming Sitemaphttps://wordpress.org/support/topic/customizing-or-renaming-sitemap/
-
Separate Sitemaps per Language in Rank Mathhttps://wordpress.org/support/topic/separate-sitemaps-per-language-in-rank-math/
-
Exclude Rank math sitemaphttps://wordpress.org/support/topic/exclude-rank-math-sitemap/
-
Consider updating the NGINX snippethttps://wordpress.org/support/topic/consider-updating-the-nginx-snippet/
-
Adding to RankMath’s default robots.txthttps://wordpress.org/support/topic/adding-to-rankmaths-default-robots-txt/
-
Include multilingual homepages in sitemaphttps://wordpress.org/support/topic/include-multilingual-homepages-in-sitemap/
-
The site map links does not included in rewrite rules for heartbeat requestshttps://wordpress.org/support/topic/the-site-map-links-does-not-included-in-rewrite-rules-for-heartbeat-requests/
-
Sitemap editinghttps://wordpress.org/support/topic/sitemap-editing-2/
-
How to Include Images (fave_taxonomy_img) in Property Area Taxonomyhttps://wordpress.org/support/topic/how-to-include-images-fave_taxonomy_img-in-property-area-taxonomy/
-
how often is sitemap action called?https://wordpress.org/support/topic/how-often-is-sitemap-action-called/
-
add external Sitemap Link is not workinghttps://wordpress.org/support/topic/add-external-sitemap-link-is-not-working/
-
Automated post sitemap updatehttps://wordpress.org/support/topic/automated-post-sitemap-update/
-
[RE] Rank Math doesn’t include Categories Taxonomies from other Languageshttps://wordpress.org/support/topic/re-rank-math-doesnt-include-categories-taxonomies-from-other-languages/
-
how to set up custom site map slug?https://wordpress.org/support/topic/how-to-set-up-custom-site-map-slug/
-
Sitemaps in multilingual websiteshttps://wordpress.org/support/topic/sitemaps-in-multilingual-websites/
-
Can I edit function sitemap_images($url)https://wordpress.org/support/topic/can-i-edit-function-sitemap_imagesurl/