2

I was curious to know if it was possible to somehow configure nginx so that it will parse url arguments without specifying .php at the end of the filename before the arguments are sent.

For example, let's say I have an account module which processes which aspect of the account to load, based on the argument sent. If the argument is ?sk='login', it will load the login module. If it is ?sk='register', it will load the registration module - and so on and so forth.

The problem is that when I type http://host/account?sk='login', I do not get anything via $_GET when I try to print the values within the array. The thing is that the clean URL does load the file which is supposed to manage the $_GET arguments, except it will not actually process $_GET unless I specify .php at the end of the filename.

I'm guessing there's an nginx or php-fpm configuration somehow which allows this.

Is this possible?

zeboidlund
  • 9,731
  • 31
  • 118
  • 180

1 Answers1

1

I figured it out:

The idea, with nginx, is to define a location, specify that location's root, and then do a try_files while specifying the $uri variable and the .php file to execute, with the arguments.

For example:

location /somelocation {
    root /path/to/somelocation/on/server;
    try_files $uri /somelocation/somefile.php?key=$args;
}

Rather than typing http://host/somelocation?key='someargument', you would specify /somelocation?someargument

From there on, when you call $_GET[ 'key' ] in your php, it will output someargument with a key of key. The key can be whatever you specify.

zeboidlund
  • 9,731
  • 31
  • 118
  • 180
  • Be sure to read this before implementing this answer https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/#root-inside-location-block – Henno Jul 01 '16 at 20:21