Hello. There is a site in whose location I want to make a rewrite to another page in case the link ends in .php

For example, if we want to do so http://www.site.com/new-inventory/getfile.php then we will have to go to http://www.site.com/this-new-rewrited-page

Did so, but it does not work.

location /new-inventory { rewrite .*\.php /this-new-rewrited-page; }

So did too:

location ~ /new-inventory/.*\.php { rewrite .* /this-new-rewrited-page; }

Both options do not work, gives 404, as if I request a file that does not exist. Yes, I am requesting a non-existent file, but a check on whether the file exists and an error should not be performed BEFORE operations in the locale are executed. Tell me what I'm doing wrong?

location / { rewrite /new-inventory/.*\.php /this-new-rewrited-page last; rewrite ^(.*)$ /index.php last; }

My Location. He wrote it in it.

  • Next you have a config, probably, location / {...} goes. Actually, the last and fulfills it. Those. it is necessary to write checks already inside this location . You can catch earlier location = /this-new-rewrited-page {...} - specify the exact match. In general, too little information. Just now, I was pulling information from one friend to solve his problem . - romeo
  • I added to the post as I tried to make changes in / location. - Mihail Politaev
  • And when I do rewrite without php at the end rewrite /new-inventory/.* /this-new-rewrited-page last; it works. It is worth adding at the end \.php - no. As if nginx understands that this is a file and immediately tries to find it in this location. We need to give him to understand that he did not try to find this file. - Mihail Politaev

1 answer 1

Try

 location /new-inventory { try_files $uri /this-new-rewrited-page; }