.htaccess (x2) 到 nginx 重写 - 子目录中的不同规则 (Razor CMS)

.htacces (x2) to nginx rewrite - different rules in subdirectory (RazorCMS)

问题是关于翻译 RazorCMS (razorcms.co.uk) 重写规则,但我认为答案可以是笼统的。

我有两个 .htacces 文件。首先是在主目录 (/) 中。我翻译了:

RewriteRule ^(.*)$ index.php?path= [L,QSA]

location / { try_files $uri $uri/ @rewrite; }
location @rewrite { rewrite ^/(.*)$ /index.php?path=; }

这个没有问题

第二个 .htacces 文件位于子目录 (/rars) 中。 里面的内容是:

RewriteRule ^login/u/(.*)/p/(.*)$ index.php?login=1&u=&p= [L,QSA]
RewriteRule ^login index.php?login=1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?path= [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L,QSA]

我试着把它翻译成不同的规则:

location /login { rewrite ^/login/u/(.*)/p/(.*)$ /index.php?login=1&u=&p= break; }
location /login { rewrite ^/login /index.php?login=1 break; }
location /rars { if (!-e $request_filename){ rewrite ^(.*)$ /index.php?path=; } }

...但它不起作用。

如果第二个 .htaccess 位于子目录中,我如何将 .htaccess 规则转换为 Nginx 重写?你能给我指出正确的方向吗?

Nginx 没有每个目录的上下文,因此您需要提供该位置的完整路径。尝试:

location /rars/login { rewrite ^/rars/login/u/(.*)/p/(.*)$ /rars/index.php?login=1&u=&p= break; }
location /rars/login { rewrite ^/rars/login$ /rars/index.php?login=1 break; }
location /rars { if (!-e $request_filename){ rewrite ^/rars/(.*)$ /rars/index.php?path=; } }