• Our team is looking to connect with folks who use email services provided by Plesk, or a premium service. If you'd like to be part of the discovery process and share your experiences, we invite you to complete this short screening survey. If your responses match the persona we are looking for, you'll receive a link to schedule a call at your convenience. We look forward to hearing from you!
  • We are looking for U.S.-based freelancer or agency working with SEO or WordPress for a quick 30-min interviews to gather feedback on XOVI, a successful German SEO tool we’re looking to launch in the U.S.
    If you qualify and participate, you’ll receive a $30 Amazon gift card as a thank-you. Please apply here. Thanks for helping shape a better SEO product for agencies!
  • The BIND DNS server has already been deprecated and removed from Plesk for Windows.
    If a Plesk for Windows server is still using BIND, the upgrade to Plesk Obsidian 18.0.70 will be unavailable until the administrator switches the DNS server to Microsoft DNS. We strongly recommend transitioning to Microsoft DNS within the next 6 weeks, before the Plesk 18.0.70 release.
  • The Horde component is removed from Plesk Installer. We recommend switching to another webmail software supported in Plesk.

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