Nginx:前端控制器重写规则中的问号

时间:2012-10-17 15:23:02

标签: php mod-rewrite url-rewriting nginx rewrite

目前我有这段代码:

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

这适用于以下情况: example.com/foo重定向到index.php

但是example.com/foo?bar不起作用。你是如何让它发挥作用的?

FWIW:我没有在Apache的mod_rewrite等价物中遇到这个问题。基本上,我将一个从Apache运行到Nginx的站点移动了。现在我遇到了这个问题。

修改

要清楚这是我要做的事情:

  • example.com/foo
  • example.com/foo/bar/etc
  • example.com/foo?bar
  • example.com/foo?bar=quz

所有人都应“默默地”提供index.php“而不更改浏览器地址栏的网址。

1 个答案:

答案 0 :(得分:1)

我刚刚使用以下配置对其进行了测试,我相信这确实是您想要的:

server {
#listen   80; ## listen for ipv4; this line is default and implied
#listen   [::]:80 default ipv6only=on; ## listen for ipv6

root /home/www/test;
index index.php index.html index.htm;

# Make site accessible from 
server_name test.myhost.nl;

location / {        
    # First attempt to serve request as file, then as directory, then fall back to index.php
    try_files $uri $uri/ /index.php?$args;
}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
    try_files $uri = 404;

    # Fix for server variables that behave differently under nginx/php-fpm than typically expected
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    # Include the standard fastcgi_params file included with ngingx
    include fastcgi_params;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    fastcgi_index index.php;

    # Override the SCRIPT_FILENAME variable set by fastcgi_params
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

    # Pass to upstream PHP-FPM; This must match whater you name your upstream connection
    #fastcgi_pass phpfpm;
    fastcgi_pass 127.0.0.1:9000;
}
}