Score:2

Apache .htaccess to NGINX RewriteRules Port

cn flag

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 avatar
cn flag
Please help me somebody, I am so much tired of trying :(
Score:1
fr flag

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

NoobAvi avatar
cn flag
Thanks. Working absolutely great.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.