Convert URL segments to Query String parameters and pass to PHP
September 30, 2013 09:52AM
I'm migrating an application from Apache2 to nginx and having trouble configuring my server.

What it should do is:
1. If the requested URI is a real file or directory, serve it.
2. If not, and it is the root, /, execute the file Director.php in the web root using a fastcgi_pass.
3. If the URL has one segment, ie. "mydomain.com/SomeWhere/", then execute the file Director.php in the web root using a fastcgi_pass, and set the Query String parameter named rt equal to "SomeWhere". ie. Director.php?rt=SomeWhere
4. If the URL has 2 segments, ie. "mydomain.com/SomeWhere/Else/" , then execute the file Director.php in the web root using a fastcgi_pass, and set the Query String parameter named "rt" equal to "SomeWhere" and the Query String parameter named "action" equal to "Else". ie. Director.php?rt=SomeWhere&action=Else.

In all of the above cases, if the URI already has Query String parameters, they should be merged together. ie. mydomain.com/SomeWhere/Else/?ID=7 should become Director.php?rt=SomeWhere&action=Else&ID=7

Soem people/places have suguested using regualr expression location blocks, and some have sugguested using rewrite rules. I haven't been able to get either method to work for me. The best I've gotten so far is the message "No input file specified. " on every request.

I currently have this:

location /([a-zA-Z0-9\-\_]+)/ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index Director.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param QUERY_STRING rt=$1;
include fastcgi_params;
}

location / {
try_files $uri $uri/ /Director.php;
}


My working Apache rules were:

DirectoryIndex Director.php
RewriteRule ^/([a-zA-Z0-9\-\_]+)/$ /Director.php?rt=$1 [L,QSA]
RewriteRule ^/([a-zA-Z0-9\-\_]+)/([a-zA-Z0-9\-\_]+)/$ /Director.php?rt=$1&action=$2 [L,QSA]
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 101
Record Number of Users: 6 on February 13, 2018
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready