Sitemap by Yoast’s WordPress-SEO

Following are Nginx rewrite rules we are using for sitemaps generated by <a href="http://wordpress.org/extend/plugins/wordpress-seo/">WordPress-SEO by Yoast</a> plugin on different sites.
#Yoast sitemap
location ~ ([^/]*)sitemap(.*).x(m|s)l$ {
    rewrite ^/sitemap.xml$ /sitemap_index.xml permanent;
        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;
        ## following lines are options. Needed for wordpress-seo addons
        rewrite ^/news_sitemap.xml$ /index.php?sitemap=wpseo_news last;
    rewrite ^/locations.kml$ /index.php?sitemap=wpseo_local_kml last;
    rewrite ^/geo_sitemap.xml$ /index.php?sitemap=wpseo_local last;
    rewrite ^/video-sitemap.xsl$ /index.php?xsl=video last;
    access_log off;
}

Notes

  1. First line is to redirect old sitemap.xml to sitemap_index.xml. You can remove it if you want.
  2. Few lines are added for WordPress-SEO addons. You can uncomment them based on addons you are using.
  3. We have test rewrite rules for news sitemap only for real. If you find any rules broken, feel free to add it via comment below and we will have it fixed asap.
  4. access_log off can be removed if you are interested to keep log of sitemap requests. We disable it to save some disk I/O.
  5. If your site-content is not updated frequently (many times in an hour), you may cache sitemap using fastcgi-cache.

More: Checkout our 50+ WordPress-Nginx Tutorials

20 responses to “Sitemap by Yoast’s WordPress-SEO”

  1. Hi,

    I’m trying to figure out where (which file) to put the code in?

    • /etc/nginx/sites-available/mywordpress
    • /etc/nginx/nginx.conf

    Let me know, thanks.

    • You need to find out file in which there is server block for your site.

      If your site’s domain is example.com, look for file contianing line like:

      server_name example.com

      Then inside server block, you can paste your locations.

      • Rahul,

        Thanks. Pasted this in the ‘sites-available’ file under server block. My sitemap seems to be working. The Yoast plugin is still showing me the error. Strange but I’ll probably survive.

  2. I am using Easy Engine, and I noticed the sitemap is already working. Do i need to alter anything?

      • Actually, without put manually this rules even with EE, can’t access the sitemap_index.xml it’s a bug maybe ?

        • By default easyengine doesn’t install any SEO plugin and don’t write nginx rules.

          In WordPress community there are 2000+ plugins and easyengine doesn’t add nginx rules which is not installed by easyengine.

          If you install any SEO plugin then you have to manually add/change nginx rules for that particular plugin.

  3. Hi,
    I use a different SEO plugin, so I would prefer to have ‘Google XML Sitemaps’ plugin delivering the file.

    I’ve tried pasting this code, but to no avail.

    Could you recommend another practice to serve a sitemap with oither plugins please?

  4. Even after adding the above code(yes i have added them at the right place) Yoast SEO plugin keeps showing me a message that as you are on NGINX you should add this rewrite.
    And even when i tried to access the sitemap it shows me error saying that it failed to load the XSL stylesheet,however it used to work fine in Apache,But when i tested the sitemap from Google webmaster it doesn’t shows any error.
    What does stylesheet has to do with this?

  5. Hello,

    I use Nginx, I added these new rewrite rules, but no sitemap is generated. Is can be a problem of permission on server? Can you advice me what settings change?

    Thank you

  6. My installation has generated xml without any rewrites at all.
    I don’t have the ones mentioned here nor the ones recommended via the yoast seo settings page. Really weird.

    I can navigate to the sitemap_index.xml and see it contains my page, category and other xml files.

    Could I be missing something crucial by NOT including these rewrites?

    Below is my vhost file in site-enabled

    server {
    server_name http://www.example.com;
    listen xxx.xxx.xxx.xxx:80;
    return 301 http://example.com$request_uri;
    }
    server {
    listen xxx.xxx.xxx.xxx:80;
    server_name example.com;
    charset utf8;
    access_log /var/log/nginx/example_access.log;
    error_log /var/log/nginx/example_error.log;
    root /var/www/example.com/htdocs;
    # BEGIN Better WP Security
    if ($http_cookie !~* “wordpress_logged_in_” ) {
    set $susquery 2$susquery;
    set $rule_2 1;
    set $rule_3 1;
    }
    if ($args !~ “^loggedout=true”) { set $susquery 3$susquery; }
    # END Better WP Security
    client_max_body_size 25M;
    fastcgi_buffers 64 4K;
    index index.php;
    location / {
    try_files $uri $uri/ /index.php?$args;
    }
    location ~ \.php$ {
    try_files $uri =404;
    include fastcgi_params;
    fastcgi_pass 127.0.0.1:9000;
    }
    location ~ /\.ht {
    deny all;
    }
    }

  7. I added the rewrites to my site’s vhost config file, rebooted my server, restarted NGINX — when I visit /sitemap_index.xml – it’s just a blank white page with no sitemap info.

    Any ideas?

  8. rewrite ^/sitemap\.xsl$ /index.php?xsl=1 last;

    should read

    rewrite ^/([a-z]+)?-?sitemap\.xsl$ /index.php?xsl=$1 last;