Apache .htaccess 到 NGINX RewriteRules 端口

Apache .htaccess 到 NGINX RewriteRules 端口

所以,我实际上正在尝试移植RewriteRules 来自阿帕奇NGINX但似乎我无法完全移植。

实际上,我的服务器上确实有一个在服务器上的https://example.com域和/var/www/html/路径上运行的站点。我试图做的是在var/www/html/subdirectory路径和域下的子目录中安装自定义脚本https://example.com/subdirectory

问题是重写规则不起作用,甚至出现 404 未找到错误。请帮助我。

我的 Apache.htaccess文件:

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]

NGINX config我尝试移植的文件:

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

  }
}

答案1

这应该适用于您的情况。灵感来自可用的解决方案@https://serversforhackers.com/c/nginx-php-in-subdirectory

最终配置文件:

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;

  }

}

如有任何疑问,请询问。另外,感谢您提出这样的问题,因为我也是新手,所以我学到了一些新东西:P;干杯!!

相关内容