Best practice for URL rewriting with php parameter

Francis Daly francis at daoine.org
Wed Nov 11 19:38:27 UTC 2015


On Wed, Nov 11, 2015 at 06:50:54PM +0000, Mik J wrote:

Hi there,

> I tried both methods but none of them worked. I'm going to look at it more in details (and display the php logs because I just had a blank page).

First configure things so that an explicit request for
/index.php?x=information gives you the response that you want.

Keep it simple, do one thing at a time.

> Also I would like to know why the solution you're offering is a "best practice" ?At first it seems a bit heavy because I'll have a paragraph like that for every x variable (x=information, x=something).

The original question did not have multiple x variables. A different
question probably gets a different answer.

Using "rewrite" instead of "try_files" should have exactly the same
number of location{} blocks.

And it avoids a presumed-unnecessary filesystem check -- what should
happen if the file /usr/local/nginx/htmlinformation exists on your
filesystem?

Using "fastcgi_pass" and friends instead of "rewrite" should also have
exactly the same number of location{} blocks. And it means that nginx
can skip one step of processing -- if the rewrite was going to go to a
location which does exactly this anyway.

It does mean that there are more words involved; but if the number of
location{} block is going to be big, you are probably going to generate
them anyway.

> Considering also that the users URL might not look like the x variable.nginx.org/info_1234 is an internal rewrite of index.php?x=information

I don't see the difference this makes, in the three cases in question.

In each case you have the request, and a different string as the x= value.

If you choose to use variables in a common location, or something like
that, then you will still need the mapping or request to x= value. If you
knew that the mapping is always "remove the first /", then you wouldn't
need a separate table. But you don't have that now, either.


> I tried two other solutions that workedrewrite /info_1234 /index.php?x=information;
> and
> location = /info_1234 { try_files info_1234 /index.php?x=information; }
> I would like to understand why your solution is better than these, why is it a best practice ?

It means that nginx has less work to do to process the request.

Sometime that is not the thing that you want to optimise for.

	f
-- 
Francis Daly        francis at daoine.org



More information about the nginx mailing list