【问题标题】:.htacces (x2) to nginx rewrite - different rules in subdirectory (RazorCMS).htaccess (x2) to nginx rewrite - 子目录中的不同规则(Razor CMS)
【发布时间】:2015-12-04 18:53:11
【问题描述】:

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

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

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

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

这个没问题。

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

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

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

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

...但它不起作用。

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

【问题讨论】:

    标签: .htaccess nginx url-rewriting


    【解决方案1】:

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

    location /rars/login { rewrite ^/rars/login/u/(.*)/p/(.*)$ /rars/index.php?login=1&u=$1&p=$2 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=$1; } }
    

    【讨论】:

    • 谢谢。这很有帮助。我将配置更改为:location /login { rewrite ^/login/u/(.*)/p/(.*)$ /rars/index.php?login=1&u=$1&p=$2 break; rewrite ^/login$ /rars/index.php?login=1 break; } location /rars { rewrite ^/rars/(.*)$ /rars/index.php?path=$1 last;} ...它可以正常工作,但是如果我转到 /login 而不是执行 /rars/index.php?login=1 nginx 会向我发送包含 php 代码的“登录”文件。仍在寻找配置中的一些错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多