0

I want to make a Nginx rewrite rule for parameters pretending to be top-level directories. For instance, I want example.org/myval to be redirected to example.org/foo.php?param=myval, but at the top level I already have some other files (like index.php) I would like to be reachable.

My attempt is:

location = ^/(?!.*)$ {
  rewrite ^/$ https://example.org/index.php break;
}

location = ^/(.*) {
  rewrite /(.*) /foo.php?param=$1 last;
}

the 1st rule catching only root, the second trying to catch pretending top-levels (but failing),so all I get when accessing non-(something.php) files is a 404. Any ideas?

EDIT: Later I have the PHP block:

location ~ ^/(index|signup|login|).php$ {
  include snippets/fastcgi-php.conf;
  fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}

which I've come to know contains the try_files part (but shouldn't match anything else); I don't know where does the 404 come from.

  • I would think that `location = / {}` would be enough for root and would [take priority](https://stackoverflow.com/a/5238430/231316). – Chris Haas Feb 07 '23 at 14:17
  • the /myval site still 404s though (and I like it to hide the "index.php" part) – gaboflowers Feb 07 '23 at 15:31
  • Personally, I wouldn't give a second thought to "hiding" index.php, just never link to it. If someone happens to hack the URL and put that in, it isn't worth fighting. If you are worried about SEO, as noted, don't link to it, but just add a canonical header and you're good. – Chris Haas Feb 07 '23 at 16:03

0 Answers0