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
- First line is to redirect old
sitemap.xml
to sitemap_index.xml
. You can remove it if you want.
- Few lines are added for WordPress-SEO addons. You can uncomment them based on addons you are using.
- 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.
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.
- 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
Hi,
I’m trying to figure out where (which file) to put the code in?
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: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.
I am using Easy Engine, and I noticed the sitemap is already working. Do i need to alter anything?
If its working fine with EasyEngine, then no need to do anything else.
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.
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?
I haven’t used other plugins but if they provide their own
.htaccess
code snippets, you may try converting them to nginx equivalent.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?
Just install and activate nginx-helper plugin – http://wordpress.org/plugins/nginx-helper/
You can disable all it’s settings if you don’t need them. I think that warning will go away.
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
WordPress-SEO doesn’t generate static sitemaps. Try opening http://example.com/sitemap_index.xml in browser
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;
}
}
As far as I remember, Yoast SEO plugin does not generate a sitemap xml file on disk.
So check if you can open sitemap path in browser. For example: https://rtcamp.com/sitemap_index.xml (for this site)
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?
rewrite ^/sitemap\.xsl$ /index.php?xsl=1 last;
should read
rewrite ^/([a-z]+)?-?sitemap\.xsl$ /index.php?xsl=$1 last;
Thanks for correction. 🙂
i need to rewrite sitemap.xml to sitemap_index.xml
how to redirect using nginx virtualhost
Please refer to our community forums for all support related questions.
Link: https://community.easyengine.io/