• Please be aware: Kaspersky Anti-Virus has been deprecated
    With the upgrade to Plesk Obsidian 18.0.64, "Kaspersky Anti-Virus for Servers" will be automatically removed from the servers it is installed on. We recommend that you migrate to Sophos Anti-Virus for Servers.
  • The Horde webmail has been deprecated. Its complete removal is scheduled for April 2025. For details and recommended actions, see the Feature and Deprecation Plan.

Resolved How to avoid redirection loop in plesk nginx config?

KurtLowe

New Pleskian
Server operating system version
Ubuntu 24.04.1
Plesk version and microupdate number
18.0.64
Hi All,

Through Plesk, I am trying to modify my nginx additional directives so that "/sitemap.php" redirects to "/sitemap.xml". When going to "/sitemap.xml" it should serve the page with "/sitemap.php". I have written the location blocks below:

location ~ ^/sitemap.xml {
try_files $uri /sitemap.php;
}

location ~ ^/sitemap.php {
return 301 /sitemap.xml;
}

This causes a redirect loop back and forward between /sitemap.xml and /sitemap.php. I researched to find some methods to prevent this, such as adding last after the try_files statement or adding a check to not redirect with a 301 if the user is already at the sitemap.php page. These would work on other systems, however seem to be overriding some of the config plesk has already written, causing my PHP page to get downloaded instead of displayed.

Does anyone know how I can fix this problem when writing my nginx.conf in a plesk environment? Help would be much appreciated.
 
Do you have the NGINX proxy mode enabled or disabled on your server? Also, could you please confirm the PHP handler you are using?
 
Hi,

Your rewrite rule downloads the PHP file instead of executing it and returning the code output because you haven't instructed Nginx to parse the request via the PHP-FPM service. From Nginx point of view, it serves a static file.

If you wish to server PHP code from a custom location block, you will have to add all the PHP related settings like what is seen in the ``location ~ \.php(/.*)?$ {`` block.

Yoast SEO Sitemap nginx example has the location block like yours but rewrites the request to the PHP file:
Code:
location ~ ^/sitemap.xml {
rewrite ^/sitemap.xml$ /sitemap.php;
}

However, if you add the later request to redirect .php to .xml the mentioned redirect loop is seen.

I was able to redirect the sitemap.php to sitemap.xml using PHP side redirect. Add the following at the beginning of the sitemap.php script:
Code:
<?php if($_SERVER['REQUEST_URI']!='/sitemap.xml') { header("Location: /sitemap.xml");  } ?>
 
Hi,

Your rewrite rule downloads the PHP file instead of executing it and returning the code output because you haven't instructed Nginx to parse the request via the PHP-FPM service. From Nginx point of view, it serves a static file.

If you wish to server PHP code from a custom location block, you will have to add all the PHP related settings like what is seen in the ``location ~ \.php(/.*)?$ {`` block.

Yoast SEO Sitemap nginx example has the location block like yours but rewrites the request to the PHP file:
Code:
location ~ ^/sitemap.xml {
rewrite ^/sitemap.xml$ /sitemap.php;
}

However, if you add the later request to redirect .php to .xml the mentioned redirect loop is seen.

I was able to redirect the sitemap.php to sitemap.xml using PHP side redirect. Add the following at the beginning of the sitemap.php script:
Code:
<?php if($_SERVER['REQUEST_URI']!='/sitemap.xml') { header("Location: /sitemap.xml");  } ?>
This works perfectly, thank you. Out of curiosity, does the PHP redirection result in a 301 or a 302? Would this impact SEO in any way compared to using nginx instead?
 
In my example, it's a 302. However, if you add this header before the Location: header, it will be a 301:
Code:
http_response_code(301);

> Would this impact SEO in any way compared to using nginx instead?

Only the direct requests to sitemap.php are redirected by the PHP code. Hence, I don't see any SEO impact. Furthermore, in time only siteamp.xml should be indexed.
 
Back
Top