Yoast XML sitemaps on NGINX servers

Our plugin generates sitemaps dynamically when you enable the sitemap feature. In some cases, you may need to add server-level rewrites if you receive an NGINX server error or a wrong page when loading the XML sitemaps.

If you are using Apache, please use the rules here. If you are not sure which you use, please speak to your host provider. 

How do I know if I need to add the rewrite 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 version of example.com/?sitemap=1. If you can load and see the sitemap using the non-pretty permalink, your server is not set up to rewrite and, thus, you’ll need to add rewrite rules.

What are the NGINX rewrite 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 web host or server admin for assistance.

If you already have rewrites in your NGINX config file, you can add these to the same section using the basic code. If you do not have rewrite 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 rewrites 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?yoast-sitemap-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?yoast-sitemap-xsl=video last;
}

Using the code when running WordPress in a subfolder (or with the multisite WordPress installation with the subfolder domain mapping enabled)

location ~ ([^/]*)sitemap(.*).x(m|s)l$ { 
  ## this makes the XML sitemaps work 
  rewrite ^.*/([a-z]+)?-?sitemap.xsl$ /index.php?yoast-sitemap-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; 
  # Rules for yoast    sitemap with wp|wpsubdir|wpsubdomain 
  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?yoast-sitemap-xsl=video last; 
}

Still not working?

If you’ve added the redirects above and the pretty permalinks do not load, the issue is likely to do with other settings on your server. Please contact your web host or server admin for assistance.

Get free SEO tips!