大网数据广告活动
服务器运维问答

Apache .htaccess to NGINX RewriteRules Port

· 52CE 我爱测

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 th…

问题原文

原帖链接 · NoobAvi · CC BY-SA 4.0

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

  }
}

已采纳解答

原帖链接 · OMi Shah · CC BY-SA 4.0

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

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2021-07-10)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。