【问题标题】:PHP cant see query string from RewriteRulePHP 看不到来自 RewriteRule 的查询字符串
【发布时间】:2016-03-15 06:17:06
【问题描述】:

我的.htaccess 看起来像

RewriteEngine On
RewriteRule ^api/([^/]*)$ /api.php?method=$1 [L,QSA]

我的api.php 看起来像

class API {

    public function __construct()
    {
        require_once('helpers.php');
    }

    public function test()
    {
        dd('hey');
    }

};

$api = new API;
$method = isset( $_GET['method'] ) ? $_GET['method'] : null;

dd($_REQUEST, $_GET);

if( $method && method_exists($api, $method) ){
    $api->{$method}();
}
else {
    exit("Nothing to see here governor.");
}

然而,当我访问重写的网址时,例如site.com/api/test我明白了

array (size=0)
  empty

如果我将其更改为 /api.php?method=test/api/test?method=test 我得到

array (size=1)
  'method' => string 'test' (length=4)

为什么无法检测到查询字符串?

服务器设置为 apache 2.2, php-fmt 5.6

谢谢

【问题讨论】:

    标签: php .htaccess mod-rewrite query-string


    【解决方案1】:

    由于以api 开头的URI 是相同的PHP 处理程序api.php 我怀疑这是MultiViews 的问题。

    在 .htaccess 开头使用此行将其关闭:

    Options -MultiViews
    

    MultiViews 选项由 Apache 的 content negotiation 模块使用,该模块运行 before mod_rewrite 并使 Apache 服务器匹配文件的扩展名。所以/api 可以在 URL 中,但它会服务于/api.php

    【讨论】:

    • 确认一下,这确实解决了我的问题!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-23
    相关资源
    最近更新 更多