A pretty URL for a specific PHP page?
Francis Daly
francis at daoine.org
Fri Sep 10 21:31:30 UTC 2021
On Mon, Sep 06, 2021 at 02:58:37PM +1000, Jore wrote:
Hi there,
> Is it possible to make a pretty URL for a specific PHP page?
Some php scripts make it straightforward, by handling that themselves. I
guess that yours is not one of those?
> I'd like https://domain.com/report/ to render
> https://domain.com/index.php?r=app%2Fform&id=2lyEsw
The simple thing -- that you possibly do not want to do here? -- would be
to redirect to the desired url; in that case, the browser would make the
"pretty" request, get the redirect response, make the "real" request,
and get the full response. That would be something like
location = /report/ { return 301 /index.php?r=app%2Fform&id=2lyEsw; }
> So I tried this location block, but I get 403 forbidden:
>
> location /report {
> index index.php?r=app%2Fform&id=2lyEsw;
> alias /var/www/easyforms;
> }
"index" expects a filename argument; when that fails, it will probably
try a directory index of /var/www/easyforms, which presumably leads to
the 403 here.
What you can try, is to call fastcgi_pass with appropriate parameters,
directly.
The exact parameters wanted by your fastcgi server and by your php script,
depend on what they expect. And if you send the same parameter name more
than once, whether the fastcgi server will provide the first, the last,
or all amalgamated, to the php script, depends on the fastcgi server.
But often "SCRIPT_FILENAME" and "QUERY_STRING" are enough to start with;
if you need other things from your fastcgi.conf or fastcgi_params files,
you can "include" those too, in a place where the manually-provided
variables are not overridden.
So the following might have a chance of working as you want; and if
it does not, then the fastcgi error log or the nginx (debug) error log
might help point at other changes that might be needed.
location = /report/ {
fastcgi_pass unix:/run/php/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
fastcgi_param QUERY_STRING r=app%2Fform&id=2lyEsw;
}
One extra note: if the returned content includes relative links --
something like "img src=new.png"-- then the browser will make a different
follow-up request for it if the browser asked for "/report/", and if
the browser asked for "/index.php?anything". If your output shows some
broken-image links or the like, that might be a thing to check for. But
that only matters after the rest of the config is working.
Good luck with it!
f
--
Francis Daly francis at daoine.org
More information about the nginx
mailing list