Best practice for URL rewriting with php parameter

Francis Daly francis at daoine.org
Sun Nov 15 12:26:26 UTC 2015


On Thu, Nov 12, 2015 at 11:04:50PM +0000, Mik J wrote:

Hi there,

> I tried again your solution from yesterday and didn't manage to make it work

If you have one solution that works well enough for you, use it. It is
usually not worth spending more time preparing something than will be
saved by using it.

>  location = /information { rewrite ^ /index.php?x=information; }

That should work as a straight drop-in replacement for your initial
try_files configuration.

If that does not work, then there may be something to investigate.

>   location = /index.php {
>     fastcgi_pass 127.0.0.1:9000;    fastcgi_param SCRIPT_FILENAME $document_root$uri;
>     fastcgi_param QUERY_STRING $query_string;
>   }

That is based on me guessing what your actual configuration has.

Somewhere in your configuration, you have a location{} that handles the
request for /index.php

Adjust this location to match that one; or just use that one directly.

>   location = /information {
>     fastcgi_pass 127.0.0.1:9000;    fastcgi_param SCRIPT_FILENAME $document_root/index.php;
>     fastcgi_param QUERY_STRING x=information;
>   }

This one does the fastcgi_pass directly, avoiding the rewrite or the
internal redirection.

Again, the actual configuration required depends on the part of
your config that you have not shown. Adjust it to match your local
requirements, if you want to investigate further.


> However when the x variable can take multiple values x=information or x=something, the second solution you wrote seems to be long. If I had to implement as you suggested, I'd have
> location = /information { rewrite ^ /index.php?x=information; }location = /something { rewrite ^ /index.php?x=something; }  location = /index.php {
>     fastcgi_pass 127.0.0.1:9000;    fastcgi_param SCRIPT_FILENAME $document_root$uri;
>     fastcgi_param QUERY_STRING $query_string;
>   }

I'm afraid that I don't understand this part of your response.

It seems like you are saying that you are happy to currently write

  location = /one { # one try_files directive }

  location = /two { # one try_files directive }

but you think that writing

  location = /one { # one rewrite directive }

  location = /two { # one rewrite directive }

is too long? I suspect that I have misunderstood you.

Alternatively, you think that writing

  location = /one { # three or four fastcgi-related directives }

  location = /two { # three or four fastcgi-related directives }

is too long? In that case, you must balance "more words for you to put
into the config file" with "less work for nginx to do to process the
request"; and you must decide where on that scale you want this server
to fit.

I think that nginx best practice leans towards "less work
for nginx to do to process the request". I think that that is
because person-configuration-writing time has zero cost, and
machine-request-processing time has nonzero cost.

(person-configuration-reading and person-configuration-modifying
times also matter; I suspect that they point at "less work for
nginx..." too. But it's your system; you choose how you want to manage
it.)

Good luck with it,

	f
-- 
Francis Daly        francis at daoine.org



More information about the nginx mailing list