Apache .htaccess to NGINX RewriteRules Port
This should work for your case. Inspired from the solution available @ https://serversforhackers.com/c/nginx-php-in-subdirectory
Final config file:
server
{
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.php index.html;
server_name localhost;
location /
{
try_files $uri $uri/ =404;
}
# pass PHP scripts to FastCGI server
location ~ \.php$
{
include snippets/fastcgi-php.conf;
# # With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
}
# for the sub-directory
location /subdirecory
{
alias /var/www/html/subdirectory;
try_files $uri $uri/ @subdirectory; # send all the request to @subdirectory tagged location
location ~ \.php$
{
include snippets/fastcgi-php.conf;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
}
location @subdirectory
{
rewrite /subdirectory/about$ /subdirectory/pages/about.php last;
rewrite /subdirectory/privacy-policy$ /subdirectory/pages/privacy-policy.php last;
rewrite /subdirectory/contact$ /subdirectory/pages/contact.php last;
rewrite /subdirectory/page$ /subdirectory/pages/page.php last;
rewrite /subdirectory/terms$ /subdirectory/pages/tos.php last;
rewrite ^/subdirectory/sitemap-([0-9]+).xml$ /subdirectory/parts/sitemaps/sitemap-$1.xml last;
rewrite ^/subdirectory/(.*)/(.*)/(.*)/(.*)/?$ /subdirectory/index.php?bank=$1&state=$2&district=$3&branch=$4 last;
rewrite ^/subdirectory/(.*)/(.*)/(.*)/?$ /subdirectory/index.php?bank=$1&state=$2&district=$3 last;
rewrite ^/subdirectory/(.*)/(.*)/?$ /subdirectory/index.php?bank=$1&state=$2 last;
rewrite ^/subdirectory/(.*)/?$ /subdirectory/index.php?bank=$1 last;
}
}
For any doubts kindly ask. Also, thanks to you for such a question, I got to learn something new since I am new too :P; Cheers !!