Welcome! Log In Create A New Profile


Need to pass a request for sitemap_index.xml to the php backend and avoid nginx rewrite rules

Posted by edbloom 
Hi there,

I'm trying to setup the Yoast WordPress SEO plugin's sitemap functionality to place nice with nginx and php-fpm.

The plugin uses WordPress built in rewrite rule API (NOT .htaccess or nginx rule based - see here http://codex.wordpress.org/Rewrite_API/add_rewrite_rule).

Therefore it takes a request like http://mysite.com/sitemap_index.xml and rewrites this within WordPress to something like: http://mysite.com/index.php?sitemap=1

There are some other similar rewrite rules that also use the WordPress rewrite API. Here is some example code:

function init() {
$GLOBALS['wp']->add_query_var( 'sitemap' );
$GLOBALS['wp']->add_query_var( 'sitemap_n' );
add_rewrite_rule( 'sitemap_index\.xml$', 'index.php?sitemap=1', 'top' );
add_rewrite_rule( '([^/]+?)-sitemap([0-9]+)?\.xml$', 'index.php?sitemap=$matches[1]&sitemap_n=$matches[2]', 'top' );

Here is a copy of my relevant nginx conf for mysite:

# Pass PHP scripts on to PHP-FPM
location ~* \.php$ {
try_files $uri /index.php;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;

Can I create a similar rule to pass the .xml requests over to php?

At the moment it looks like nginx is trying to server a sitemap_index.xml file which actually doesn't exist.



Actually think I figured out my problem. I also had this rule in my config:

# Cache static files for as long as possible
location ~* \.(xml|ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
try_files $uri =404;
expires max;
access_log off;

I remove the xml reference and now everything works as expected.

Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 298
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready