NGINX-如何将'/ <param>'重定向到index.php?page = <param>

我在StackOverflow上阅读了很多问题,也尝试了答案,但是仍然无法满足我的需求。

我有以下用PHP编写的简单路由代码:

$page = $_GET['page'];
echo "<section id='content' class='container'>";

if ( isset($page) )
{
    switch($page)
    {
        case "guides":
            require_once("v/pages/guides/index.php");
            break;
        case "error-list-dpto":
            require_once("v/pages/error-list/error-list-dpto.php");
            break;
        //other cases here...
    }
}
echo "</section>";

我在下面有简单的nginx配置:

server {  
    listen 80;
    listen [::]:80;
    root  /app/mylogs;
    server_name localhost;

    index index.php;

    location /(.*) {
            try_files $uri $uri/ /index.php?page=$1 =404;
    }

    location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
    }

    location ~ /\.ht {
                    deny all;
    }
}

我想访问“ http://example.com/guides”之类的内容,并使$ page变量接收“ guides”一词作为值。

但是我的NGINX代码无法正常工作。当我尝试访问上述地址时,我从NGINX收到404。

我在做什么错了?

谢谢。

niewenge 回答:NGINX-如何将'/ <param>'重定向到index.php?page = <param>

我使用以下NGINX conf文件行设法实现了我所需要的:

rewrite ^/page/(.*)$ /index.php?page=$1 last;
location / {
    try_files $uri $uri/ =404;
}
本文链接:https://www.f2er.com/3065848.html

大家都在问