所以,我实际上正在尝试将RewriteRules 从Apache移植到NGINX,但似乎我无法完全移植。
实际上,在我的服务器上,我确实在服务器上的https://example.com
域和/var/www/html/
路径上有一个正在运行的站点。我要做的是在var/www/html/subdirectory
path 和 domain下的子目录中安装自定义脚本https://example.com/subdirectory
。
问题是重写规则不起作用,甚至出现 404 not found 错误。请帮助我。
我的 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;
}
}
}
这应该适用于您的情况。灵感来自可用的解决方案 @ https://serversforhackers.com/c/nginx-php-in-subdirectory
最终配置文件:
如有任何疑问,请询问。另外,感谢您提出这样的问题,因为我也是新手,所以我必须学习一些新东西:P;干杯!!