【问题标题】:Can't grab _GET params due to NGINX rewrite由于 NGINX 重写,无法获取 _GET 参数
【发布时间】:2015-05-14 21:32:39
【问题描述】:

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

所以我将我的 ?page= _GET 参数重写为类似于以下内容:website.com/page/ 并且确实有效,但是当我尝试获取我的 _GET['page'] 参数时,它返回一个键为 0 的数组,没有一个值。

我的 .htaccess 看起来像这样:

nginx 配置

charset utf-8;

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

希望有人能帮帮我!

【问题讨论】:

  • 首先,nginx没有.htaccess。二、显示完整的nginx配置

标签: php .htaccess nginx


【解决方案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. 

当您的 url 被这样格式化时,您无法获取 $_GET 参数,因为它们不再存在。

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

【讨论】:

  • 有效!实际上,我仍然能够使用 apache2 rewriterules 获取我的 _GET 参数,所以当它不适用于 nginx 时,我真的很困惑。感谢您向我展示这种方法! :)
猜你喜欢
  • 1970-01-01
  • 2015-12-05
  • 1970-01-01
  • 1970-01-01
  • 2014-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多