由于NGINX重写,无法获取_GET参数

时间:2015-03-12 16:03:37

标签: php .htaccess nginx

我的nginx .htaccess重写规则出现问题,我可以使用一些帮助,因为它会破坏我的整个网站。

所以我将我的?page= _GET参数重写为与此类似的内容:website.com/page/这实际上有效,但当我尝试抓住我的_GET ['页面&#39 ;]参数返回一个没有值的键0的数组。

我的.htaccess看起来像这样:

nginx配置

charset utf-8;

    location / {
        if (!-e $request_filename){
            rewrite ^(.*)$ /index.php?page=$1 break;
        }
    }

我希望有人可以帮助我!

1 个答案:

答案 0 :(得分:1)

尝试这样的事情:

$url = $_SERVER['REQUEST_URI'];
$parsed = parse_url($url);
$path = $parsed['path'];
$path_parts = explode('/', $path);
$desired_part = $path_parts[1]; 

这将获取您的URI段并将其展开并将它们放入数组中。然后,如果您的example.com/index.php?page=1网址变为example.com/page/1,您可以将其从$ path_parts数组中拉出来,即:

 $desired_output = $path_parts[3]; // Would return 1 in the 'example.com/page/1' example above. 

如果您的网址格式正确,您无法获取$ _GET参数,因为它们不再存在。

我建议var_dumping上面的一些变量并稍微玩一下。