Custom URL's Nginx & PHP

Wondering if anyone can help me with a problem im having setting up custom url's

Currently got an AWS EC2 instance which I've installed Nginx & PHP(FPM) onto as web server and I've configure it work fine with regular files however im being asked to serve custom url files carried over to this web server (all which are stored in a DB table).

Being relatively new to NGINX i tried a few things from threads but not sure if I were looking at the right thing.

Eventually to satisfy the needs of custom urls I semi-cheated and had PHP do all the legwork using :

error_page 404 /404.php;

Having the 404.php file reading the url and trying to look to it up in various DB tables and then redirecting to a dedicated file which loaded the correct content. I've done this with profiles & blogs and used javascript to spoof the url so it looks customer facing as they've directly to http://fake-site.com/cool-guy-profile.html

However it doesn't index properly because its a 404 with google and I want to be able to do it properly.

How do I correctly write a rule inside of a server block which says any $request_uri after the first / in the url internally goes to a file such as php file, does its business whilst maintaining the original request URI.

e.g.

Users enters https://fake-site.com/some-custom-profile-url.html, nginx catches anything after the / and passes it to a php file which uses the /some-custom-profile-url.html to look up and display correct info whilst retaining the original url.

Bad summary

fake-site.com/custom-url.html --> nginx --> php file to render content --> comes back as fake-site.com/custom-url.html

Purpose

So content created by a blog cms or a user-profile can have a custom url which is indexed by google and doesn't use my current method of using a 404 failure file to catch the not found urls.

0
задан 24 January 2018 в 11:01
2 ответа

Вам просто нужно добавить правило перезаписи в конфигурацию nginx, например :

    location /php/ {
      rewrite ^/php/(.*)  /ghp_rewr.php?$1;
    }

Любой запрос, который начинается с / php /, будет передан в скрипт php с именем ghp_rewr.php и исходный URL-адрес минус / php / будет передан как QUERY_STRING (хотя в этом нет необходимости, php также может использовать REQUEST_URI).

Не перенаправляйте!

0
ответ дан 5 December 2019 в 06:42

Способ nginx для реализации PHP-обработки ненайденных URL-адресов:

try_files $uri $uri/ /path/to/404.php;

Это указывает nginx сначала проверить, существует ли фактический URI в файловой системе. В противном случае запрос передается в файл PHP, который затем может делать все, что угодно.

0
ответ дан 5 December 2019 в 06:42

Теги

Похожие вопросы