Codeigniter 4 url rewrite on nginx giving 404 errors

  codeigniter, codeigniter-4, nginx, php, url-rewriting

I have setup a codeigniter 4 project on an nginx server. I have used subdirectories in the Controllers folder. I am trying to use dynamic rewriting to generate SEO friendly URLs but I always get Codeigniter 404 errors.

I want to rewrite the following: ->

My nginx config file for

server {


    root /var/www/;

    index index.html index.htm index.php;

    rewrite ^/india/school/([a-zA-Z0-9-]+)$ /india/school/details?p=$1 break;

    location / {

            try_files $uri $uri/ /index.php;


    location ~ .php$ {

        fastcgi_split_path_info ^(.+.php)(/.+)$;
        fastcgi_index index.php;

        include /etc/nginx/fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $request_filename;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;


    rewrite ^/india/school/([a-zA-Z0-9-]+)$ /india/school/details?p=$1 last;

    location ~ /.ht {

        deny all;


Nginx rewrite is working as per these logs:

2021/09/15 16:26:29 [notice] 122837#122837: *12273 "^/india/school/([a-zA-Z0-9-]+)$" 
matches "/india/school/permalink", server:

2021/09/15 16:26:29 [notice] 122837#122837: *12273 rewritten data: "/india/school/details", 
args: "p=permalink", server:

2021/09/15 16:26:29 [notice] 122837#122837: *12273 "^/india/school/([a-zA-Z0-9-]+)$" does 
not match "/index.php", server:

But codeigniter is giving the following error:

Controller method is not found: permalink

Please guide me on how to get the following rewrite to work.

Source: Ask PHP