Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
重写到index.php以维护查询参数_Php_Nginx_Rewrite_Slim - Fatal编程技术网

重写到index.php以维护查询参数

重写到index.php以维护查询参数,php,nginx,rewrite,slim,Php,Nginx,Rewrite,Slim,我试图将我的Nginx vhost配置为将所有请求重写到/index.php,这是框架的标准配置,但我认为Nginx没有在$\u服务器中传递URI的正确部分 我可以将/foo/bar重写为/index.php/foo/bar,路由工作正常,但如果我附加查询参数,路由将失败,因为查询参数似乎包含在请求URI字段中,而它们不应该包含在该字段中。例如,导航到http://lts3/test?foo=bar从Slim返回404错误 以下是包含参数的查询字符串(http://lts3/test?foo=b

我试图将我的Nginx vhost配置为将所有请求重写到
/index.php
,这是框架的标准配置,但我认为Nginx没有在
$\u服务器中传递URI的正确部分

我可以将
/foo/bar
重写为
/index.php/foo/bar
,路由工作正常,但如果我附加查询参数,路由将失败,因为查询参数似乎包含在
请求URI
字段中,而它们不应该包含在该字段中。例如,导航到
http://lts3/test?foo=bar
从Slim返回404错误

以下是包含参数的查询字符串(
http://lts3/test?foo=bar
),这会中断:

Array
(
    [USER] => www-data
    [HOME] => /var/www
    [FCGI_ROLE] => RESPONDER
    [QUERY_STRING] => /test?foo=bar
    [REQUEST_METHOD] => GET
    [CONTENT_TYPE] => 
    [CONTENT_LENGTH] => 
    [SCRIPT_FILENAME] => /var/www/lts3/public/index.php
    [SCRIPT_NAME] => /index.php
    [REQUEST_URI] => /test?foo=bar
    [DOCUMENT_URI] => /index.php
    [DOCUMENT_ROOT] => /var/www/lts3/public
    [SERVER_PROTOCOL] => HTTP/1.1
    [GATEWAY_INTERFACE] => CGI/1.1
    [SERVER_SOFTWARE] => nginx/1.4.1
    [REMOTE_ADDR] => 192.168.1.134
    [REMOTE_PORT] => 58050
    [SERVER_ADDR] => 192.168.1.113
    [SERVER_PORT] => 80
    [SERVER_NAME] => lts3
    [HTTPS] => 
    [REDIRECT_STATUS] => 200
    [PHP_VALUE] => include_path=.:/var/www/lts3:/usr/share/php:/usr/share/pear
    [SITEMODE] => development
    [HTTP_HOST] => lts3
    [HTTP_CONNECTION] => keep-alive
    [HTTP_ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
    [HTTP_USER_AGENT] => Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.76 Safari/537.36
    [HTTP_ACCEPT_ENCODING] => gzip,deflate,sdch
    [HTTP_ACCEPT_LANGUAGE] => en-GB,en;q=0.8
    [PHP_SELF] => /index.php
    [REQUEST_TIME_FLOAT] => 1390079833.408
    [REQUEST_TIME] => 1390079833
)
这是我的Nginx虚拟主机:

server {
        listen 80;
        server_name lts3;

        error_log /var/log/nginx/lts3-error.log;

        root /var/www/lts3/public;

        location / {
                index index.php;
                try_files $uri /index.php?$request_uri;
        }

        location ~ \.php$ {
                include fastcgi_params;
                fastcgi_param PHP_VALUE "include_path=.:/var/www/lts3:/usr/share/php:/usr/share/pear";
                fastcgi_param SITEMODE development;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
        }
}
为什么
QUERY\u STRING
REQUEST\u URI
是相同的,我如何配置我的Nginx vhost将URI的正确部分传递给每一个?我的理解是,
REQUEST\u URI
应该只包含
/test
,而
QUERY\u STRING
应该包含
foo=bar
。我尝试过各种不同的重写规则,但都没有任何积极的效果。我现在在兜圈子

我试图找到解决方案,但都无济于事。我正在使用Ubuntu 13.10。

您可能需要添加
fastcgi\u参数SCRIPT\u文件名$document\u root$fastcgi\u SCRIPT\u名称就像我在下面做的一样。我还修改了您的
try\u文件

您可能还希望包含重写的路径(我已将其注释掉)。我希望这有助于

server {
        listen 80;
        server_name lts3;

        error_log /var/log/nginx/lts3-error.log;

        root /var/www/lts3/public;

        location / {
                index index.php;
                try_files $uri $uri/ index.html;
        }

        # Optional Line (un-comment to use):
        # include /path/to/rewrites.conf;

        location ~ \.php$ {
                include fastcgi_params;
                fastcgi_param PHP_VALUE "include_path=.:/var/www/lts3:/usr/share/php:/usr/share/pear";
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_param SITEMODE development;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
        }
}
包含重写内容后,可以添加如下重写规则:

rewrite /(.+)/(.+) /index.php?$1=$2;

使用
/index.php?$request\u uri$is\u args$query\u string
如果您想附加真正的查询字符串,请在
try\u files