Yoast XML Sitemaps on NGINX servers

Last update: 03 March, 2017

Our plugin generates the sitemaps dynamically and you may need to add server level redirects if you receive an NGINX server error when loading the XML sitemaps.

How do I know if I need to add the redirect rules?

The Yoast SEO XML sitemap URL uses a pretty permalink of example.com/sitemap_index.xml but, behind the scenes, this URL also has a non-pretty permalink of example.com/?sitemap=1. If you can load and see the sitemap using the non-pretty permalink, your server is not setup to redirect and, thus, you'll need to add redirect rules.

What are the NGINX redirect rules?

Here are examples of the rewrite rules you may need on NGINX servers. If you're unsure where to add these rules, please contact your webhost or server admin for assistance.

If you already have redirects in your NGINX config file, you can add these to the same section using the basic code. If you do not have redirect rules or have an add-on like News SEO, you may need part or all of the expanded code further below.

Basic Code

# Rewrites for Yoast SEO XML Sitemap
rewrite ^/sitemap_index.xml$ /index.php?sitemap=1 last;
rewrite ^/([^/]+?)-sitemap([0-9]+)?.xml$ /index.php?sitemap=$1&sitemap_n=$2 last;

Expanded Code

#Yoast SEO Sitemaps
location ~ ([^/]*)sitemap(.*).x(m|s)l$ {
## this redirects sitemap.xml to /sitemap_index.xml
rewrite ^/sitemap.xml$ /sitemap_index.xml permanent;
## this makes the XML sitemaps work
rewrite ^/([a-z]+)?-?sitemap.xsl$ /index.php?xsl=$1 last;
rewrite ^/sitemap_index.xml$ /index.php?sitemap=1 last;
rewrite ^/([^/]+?)-sitemap([0-9]+)?.xml$ /index.php?sitemap=$1&sitemap_n=$2 last;
## The following lines are optional for the premium extensions
## News SEO
rewrite ^/news-sitemap.xml$ /index.php?sitemap=wpseo_news last;
## Local SEO
rewrite ^/locations.kml$ /index.php?sitemap=wpseo_local_kml last;
rewrite ^/geo-sitemap.xml$ /index.php?sitemap=wpseo_local last;
## Video SEO
rewrite ^/video-sitemap.xsl$ /index.php?xsl=video last;
}
Remember that you might have to modify this if you are running WordPress in a subfolder.

Was this article helpful? ·