Add Rank Math Rewrite Rules to Fix Sitemap Issue
When using Rank Math for redirects, you may find that your sitemap isn’t working as intended. You may see a notification under Rank Math > Sitemaps
or your /sitemap_index.xml
page may be blank. This is because Rank Math creates a virtual sitemap dynamically that relies on using redirects at the server-level. To fix this, you should manually add the rewrite rules provided by Rank Math for any SpinupWP sites with the Rank Math plugin installed. You’ll need to modify the Nginx config, add the code provided by Rank Math, reload the Nginx server and finally exclude the sitemap from caching.
- Modify Nginx config
- Add the complete code provided by Rank Math
- Reload the Nginx Server
- Exclude Sitemap from Caching
Modify Nginx config
When you create a website in SpinupWP, a fresh Nginx configuration file is automatically created for that website. As a sudo user, open the file and add the rewrite rules provided by Rank Math inside the server
block. The location of this file is:
/etc/nginx/sites-available/{DOMAIN}/{DOMAIN}
Add the complete Rank Math rewrite rules
Rank Math will detect that your site is on a Nginx server, notifying you with this message:
“Since you are using an NGINX server, you may need to add the following code to your configuration file if your Sitemap pages are not loading. If you are unsure how to do it, please contact your hosting provider”
The rewrite rules that come with the message are incomplete, so it’s important to add the complete rewrite rules outlined in their documentation.
The complete code as of May 2023 is:
# START Nginx Rewrites for Rank Math Sitemaps
rewrite ^/sitemap_index.xml$ /index.php?sitemap=1 last;
rewrite ^/([^/]+?)-sitemap([0-9]+)?.xml$ /index.php?sitemap=$1&sitemap_n=$2 last;
rewrite ^/([a-z]+)?-sitemap\.xsl$ /index.php?xsl=$1 last;
# END Nginx Rewrites for Rank Math Sitemaps
Reload Nginx Server
To ensure the configuration takes effect, remember to reload Nginx after making the changes to the Nginx config file:
sudo service nginx reload
Once the code has been added and the server has been reloaded, click I already added.
Exclude Sitemap from Caching
If you have caching enabled on your site, sometimes the sitemap gets cached. The sitemap should be up to date with the latest content on your site so we want to ensure it doesn’t get cached. In your SpinupWP dashboard, go to Page caching and add the following in the Path Exclusions:
/(.*)sitemap(.*).xml
/(.*)sitemap.xsl
/sitemap_index.xsl
/sitemap_index.xml
If you’re looking to add Smush and Imagify WebP support, see our Adding Smush Local WebP and Imagify WebP support doc.