Redirect 404 error to index file in subdirectory

I want to redirect 404 errors on http://test.com/folder1/ to http://test.php/folder1/index.php .

Server is running with nginx and php-fpm. Current virtual host is given below.

Read More
server {
          listen x.x.x.x:80;
          server_name test.com www.test.com;

          access_log off;
          #error_log  /dev/null;
      error_log /var/log/nginx/error.log;

          root /home/test/public_html;

          location / {
                index index.php;
                location ~.*.(3gp|gif|jpg|jpeg|png|ico|wmv|avi|asf|asx|mpg|mpeg|mp4|pls|mp3|mid|wav|swf|flv|html|htm|txt|js|css|exe|zip|tar|rar|gz|tgz|bz2|uha|7z|doc|docx|xls|xlsx|pdf|iso)$ {
                expires 7d;
                }
                location ~* .php$ {
                fastcgi_index   index.php;
                fastcgi_pass   unix:/var/run/php-fpm/test.sock;
                include         fastcgi_params;
                fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
                fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
                }
          }

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

          location ~ /.ht {
          deny all;
          }
       }

Currently 404 errors are redirecting to http://test.com/index.php . 404 errors should be redirected to index.php in every subfolder.

thanks

Related posts