0

I have a page at example.com/fruit.php. The page uses ISSET() to parse parameters to decide what to show the user. For instance, example.com/fruit.php?name=apple&color=red. This is easy enough to do but I'd rather the requests to come to the page like this:

mysite.com/fruit/apple/red

Since I know there will only be two parameters, I can easily parse the url to build my page. However, how can I configure nginx so that every request for example.com/fruit with anything behind the url should be sent to fruit.php so that the page itself can parse the url. Here is what I have tried in my location block:

location ~ /fruit {
return 301 https://www.example.com/fruit.php;
} 

This is not working for me and I've got about a day wrapped up in trying to figure it out. Any help would be appreciated.

Shawn Cooke
  • 907
  • 1
  • 11
  • 28

1 Answers1

0

I think you're approaching this wrong.

If you're trying to create and maintain routes using the web server, you're inevitably going to end up with having issues down the line such as having to to cycle your web server service on the production server everytime you deploy.

Further to this, you're tightly coupling the web server config to your application, which isn't the best thing.

What I recommend is using some sort of router package to help maintain your routes for your project.

Have a look at the answers on this stackoverflow question.

Then you can configure the web server so that it resolves to an index.php file in your project which will then use the router to handle the logic for your paths within the app.

Have a look here on what your nginx config will eventually look like: stackoverflow about nginx routing using index.php as an entry point for your requests.

Community
  • 1
  • 1
vsharper
  • 307
  • 1
  • 13
  • Is it just me or is this sort of thing m 1000% easier in node.js vs nginx? Or is it just slightly easier? – Shawn Cooke Jun 09 '16 at 21:38
  • It's true but even with NodeJS, you'll still need to use an http web server package such as express to give you some advanced routing features. Either way, a router is a necessary evil. – vsharper Jun 09 '16 at 21:42
  • Yes but `express()` is a piece of cake. Thanks for your input! – Shawn Cooke Jun 09 '16 at 21:46