2

So, I am actually trying to port the RewriteRules from Apache to NGINX but it seems I am not able to completely port out.

Actually, on my server I do have a running site on https://example.com domain and /var/www/html/ path on the server. What I am trying to do is install a custom script in a subdirectory under the var/www/html/subdirectory path and domain https://example.com/subdirectory.

The problem is rewrite rules are not working and even 404 not found errors are occuring. Kindly help me.

My Apache .htaccess file:

RewriteRule ^page/?$ pages/page.php [L]
RewriteRule ^about/?$ pages/about.php [L]
RewriteRule ^privacy-policy/?$ pages/privacy-policy.php [L]
RewriteRule ^contact/?$ pages/contact.php [L]
RewriteRule ^terms/?$ pages/tos.php [L]

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.+) - [PT,L]

RewriteRule ^sitemap-([0-9]+).xml$ parts/sitemaps/sitemap-$1.xml [QSA,L]

RewriteRule ^(.*)/(.*)/(.*)/(.*)/?$ index.php?bank=$1&state=$2&district=$3&branch=$4 [QSA,L]
RewriteRule ^(.*)/(.*)/(.*)/?$ index.php?bank=$1&state=$2&district=$3 [QSA,L]
RewriteRule ^(.*)/(.*)/?$ index.php?bank=$1&state=$2 [QSA,L]
RewriteRule ^(.*)/?$ index.php?bank=$1 [QSA,L]

and NGINX config file that I have tried to port:

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;
  }

  location /subdirectory
  {

    root /var/www/html/subdirectory;
    index index.php;
    try_files $uri $uri/ /index.php$args$query_string

    location ~ ^/(.+)
    {
    }

    location /page
    {
      rewrite ^/page/?$ /pages/page.php break;
    }

    location /about
    {
      rewrite ^/about/?$ /pages/about.php break;
    }

    location /privacy
    {
      rewrite ^/privacy-policy/?$ /pages/privacy-policy.php break;
    }

    location /contact
    {
      rewrite ^/contact/?$ /pages/contact.php break;
    }

    location /terms
    {
      rewrite ^/terms/?$ /pages/tos.php break;
    }

    location /
    {
      if (-e $request_filename)
      {
        rewrite ^/sitemap-([0-9]+).xml$ /parts/sitemaps/sitemap-$1.xml break;
      }
      rewrite ^/(.*)/(.*)/(.*)/(.*)/?$ /index.php?bank=$1&state=$2&district=$3&branch=$4 break;
      rewrite ^/(.*)/(.*)/(.*)/?$ /index.php?bank=$1&state=$2&district=$3 break;
      rewrite ^/(.*)/(.*)/?$ /index.php?bank=$1&state=$2 break;
      rewrite ^/(.*)/?$ /index.php?bank=$1 break;
    }

    location ~ /subdirectory /(.+\.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;
    }

  }
}
NoobAvi
  • 23
  • 4

1 Answers1

1

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 !!

OMi Shah
  • 126
  • 4