重写为index.php维护查询参数

时间:2014-01-18 23:11:26

标签: php nginx rewrite slim

我正在尝试配置我的Nginx vhost来重写所有对/index.php的请求,这是框架的标准,但我不认为Nginx在{{1}中传递了URI的正确部分。超全球。

我可以将$_SERVER重写为/foo/bar并且路由正常,但如果我追加查询参数,则路由失败,因为查询参数似乎包含在/index.php/foo/bar字段中,他们不应该。例如,导航到REQUEST_URI会从Slim返回404错误。

以下是包含参数(http://lts3/test?foo=bar)的查询字符串的print_r($_SERVER)结果,其中包含:

http://lts3/test?foo=bar

这是我的Nginx虚拟主机:

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
)

为什么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_STRING相同,如何配置我的Nginx vhost以将URI的正确部分传递给每个?据我了解,REQUEST_URI应仅包含REQUEST_URI/test应包含QUERY_STRING。我尝试了各种不同的重写规则,其中没有一个有任何积极的影响。我现在转圈了。

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

1 个答案:

答案 0 :(得分:1)

您可能想要添加
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;就像我在下面所做的那样。我还修改了您的try_files行。

您还可能希望包含重写路径(我已注释掉)。我希望这有帮助...

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;