1

I have recently migrated from Apache to Nginx and the performance is impeccable.
The site is a download server (mainly) with a custom PHP script.

Some example URLs are like:

https://sub.example.com
https://sub.example.com/index.php?dir=foo1/
https://sub.example.com/index.php?dir=foo2/
https://sub.example.com/index.php?dir=foo2/bar1/
https://sub.example.com/index.php?dir=foo2/bar2/

I would like to make them friendly-URLs so the above becomes:

https://sub.example.com
https://sub.example.com/foo1/
https://sub.example.com/foo2/
https://sub.example.com/foo2/bar1/
https://sub.example.com/foo2/bar2/

Please note the trailing slash our PHP script produces.
The directory is accessible with or without that slash (when manually typing URL) and needs to be accessible with or without that after the rewrite.

Any ideas on the rewrite conf for Nginx?

0
1

You could use try_files to make a difference for files which actually exist on your filesystem and URIs you like to give as parameter like yours /foo1/.

location = / {
  try_files $uri /index.php?dir=$uri;
}

In PHP this would produce:

echo $_GET['dir'];
# /foo1/
2
  • Thanks a ton. However try_files $uri index.php?dir=$uri; didn't work but try_files $uri /index.php?dir=$uri; did. Plz fix that in answer. – Umer Jan 23 '19 at 12:04
  • My apologies but the above is still incorrect. Please change it to try_files $uri /index.php?dir=$uri; I can confirm this works as intended. – Umer Jan 24 '19 at 9:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.