**FIXED** Google XML Sitemaps Nginx Rewrite Rules Not Working


#1

I believe the location for inserting the site specific nginx rewrite rules is: /var/www/website.com/nginx/

and the filename could be rewrites.conf

I want to paste the following rewrite rules:

rewrite ^/sitemap(-+([a-zA-Z0-9_-]+))?\.xml$ "/index.php?xml_sitemap=params=$2" last;
rewrite ^/sitemap(-+([a-zA-Z0-9_-]+))?\.xml\.gz$ "/index.php?xml_sitemap=params=$2;zip=true" last;
rewrite ^/sitemap(-+([a-zA-Z0-9_-]+))?\.html$ "/index.php?xml_sitemap=params=$2;html=true" last;
rewrite ^/sitemap(-+([a-zA-Z0-9_-]+))?\.html.gz$ "/index.php?xml_sitemap=params=$2;html=true;zip=true" last;
  • But simply pasting the above code into a blank nginx conf file would be enough?

  • Does the filename matter?


#2

The location is:

/var/www/domain.com/conf/nginx

All configurations files must have the extension .conf, otherwise they will be ignored by Nignx.

Once you place your configuration file in the correct location issue:

nginx -t

This will help you identify any errors in your files. If/when everything goes well just:

service nginx reload

Sometimes one needs to edit the vhost file, moving the last include some lines up in order to get the custom rewrites correctly processed:

ee site edit domain.com


#3

@portofacil Thanks for the reply.

I have done everything except the last step (editing the vhost file). Till now, no changes are being reflected.

(I am trying to make the Google XML Sitemaps plugin work.)

Simply doing ee site edit domain.com would reset or change anything? I am using Let’sEncrypt and wpredis. Just wanted to confirm this before trying out this step.


#4

Usually it is safe to edit the vhost file, if you just want to change the order of the includes.


#5

Tried editing vhost file by moving the include statement a couple of lines above. Restarted nginx.

The changes have still not taken place. (the sitemap still doesn’t work). Tried saving the permalinks as well.


#6

Edited file:

server {


    server_name domain.com   www.domain.com;


    access_log /var/log/nginx/domain.com.access.log rt_cache_redis;
    error_log /var/log/nginx/domain.com.error.log;


    root /var/www/domain.com/htdocs;



    index index.php index.html index.htm;

    include /var/www/domain.com/conf/nginx/*.conf;
    include  common/redis-php7.conf;

    include common/wpcommon-php7.conf;
    include common/locations-php7.conf;
}

#7

Finally Fixed!

  • Simply saved the rewrite rules into: /var/www/domain.com/conf/nginx/rewrites.conf

  • Edited the vhosts file to put the include statement a couple of lines up (made it the first include statement)

  • Checked for nginx config errors and restarted.

  • Purged cache from Nginx Helper and Redis Object Cache.

  • Saved permalinks settings without changing anything.

  • Cleared browser cache. (MOST IMPORTANT)

  • Checked domain.com/sitemap.xml. And its generated.

(Rewriting or deleting the rewrites of yoast settings in wpcommon.conf (shipped with EE) was not required)

Thanks to @portofacil


#8

I freakin’ love both of you. :sunglasses:

2 months ago I was fighting to replace /sitemap_index.xml with /sitemap.xml but to no avail. So I figured I’d just set it aside and come back to it later.

Succeeded now in a few minutes thanks to you.


#9

Also note that W3TC if being used can cause an issue due to caching .xml when using W3TC for browser caching rather than EE. http://techtutes.net/wordpress-yoast-seo-sitemap-404-error-w3-total-cache-plugin/


#10

Guys, I would like to join the Nichlas and thank you for the detailed explanation.

My sitemap works now and I am going to save the directions for the future reference.


#11