Home » Php » php – Rewrite requests to different enpoint with nginx

php – Rewrite requests to different enpoint with nginx

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have site on joomla and subfolder folder with standalone php files.

location / {
        try_files $uri $uri/ /index.php?q=$request_uri;
}
location ~ \.php$ {
        include         fastcgi_params;
        fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param HTTPS on;
        fastcgi_index   index.php;
        fastcgi_pass    backend;
        fastcgi_read_timeout 90;
    }

Now I want to do something like this:

location ~ /forder/(.*)\.php {
     try_files $uri /folder/app.php?controller=$1;
}

When I go to https://site/folder/script.php nginx send source code of that file.
Where I go wrong?

How to&Answers:

Solution for me is to put

if (!-e $request_filename){
 rewrite /folder/(.*)\.php /folder/app.php?controller=$1 last;
}

in server section