如何将nginx重写a.php,b.php,c.php到/index.php

时间:2016-01-21 07:50:52

标签: php nginx

这是我的重写代码:

    location / {                 
        if (!-e $request_filename){
            rewrite "(*UTF8)^/(.*)$" / last;
        }
    }

    location ~ \.php$ {            
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
        fastcgi_param  PHP_VALUE  "open_basedir=$document_root:/tmp/:/proc/";
    }

我想将a.php,b.php,c.php重写为/index.php。但它不起作用!但是当我输入。,bc,上面的代码可以工作时,我做错了?

1 个答案:

答案 0 :(得分:0)

您当前的问题是任何带有.php的URI都由PHP位置块处理,因此会绕过您的重写规则。在try_files位置和PHP位置使用/。例如:

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

location ~ \.php$ { 
    try_files $uri /index.php;

    fastcgi_pass   127.0.0.1:9000;
    include        fastcgi_params;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    fastcgi_param  PHP_VALUE  "open_basedir=$document_root:/tmp/:/proc/";
}

有关详情,请参阅this document