1

I'm trying to set up nginx rewrites; I have it successfully set up to remove the extension:

https://example.com/accounts?id=user-123 points to https://example.com/accounts.php?id=user-123 using just try_files:

location / { try_files $uri $uri/ $uri.html $uri.php?$args; }

I want to set up a second rewrite that goes before that; all the user IDs start with user-, so example.com/user-sdgas3 should rewrite to example.com/accounts?id=user-sdgas3, example.com/faq-abc8989as should rewrite to example.com/faqpage?id=faq-abc8989as, example.com/faq-abc8989as?view=true goes to example.com/faqpage?id=faq-abc8989as&view=true etc.

What I tried was just adding variations of this to the very beginning for each URL extension:

location ^/faq-(*)$ { try_files $faqpage?id=$1; }

with no luck. How could this be done?

ieatpizza
  • 113
  • 4

1 Answers1

0

You obviously don't need to setup a chain of rewrites (which seems like what you're trying). This would be wrong way to do things:

/user-123 -> /accounts?id=user-123 -> /accounts.php?id=user-123

... because obviously you can / should rewrite directly to the PHP handler file:

/user-123 -> /accounts.php?id=user-123

It does not seem like you have a single PHP entry-point router (the standard approach). If you had this, then try_files would be sufficient. But because you have many .php handler files, you would have to use rewrite for them.

Thus:

rewrite ^/user-(.*) /accounts.php?id=user-$1 last;
rewrite ^/faq-(.*) /faq.php?id=$1 last;

# This is only needed if /index.php can handle /this or /that route
location / {
    try_files $uri $uri /index.php$is_args$args;
} 

location \.php$ {
    ... the usual stuff for PHP-FPM
}
Danila Vershinin
  • 5,286
  • 5
  • 17
  • 21