尝试将 /directory/dir_variable/ 重写为 /directory/dir_variable/index.php

尝试将 /directory/dir_variable/ 重写为 /directory/dir_variable/index.php

我正在尝试重写这样的内容:

domain.tld/staging/base_1/ 到 domain.tld/staging/base_1/index.php,其中“1”是一个变量数。

但正在得到一个重定向循环。

*1 rewrite or internal redirection cycle while processing "/staging/base_1//index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php ....

这是我正在使用的定位指令,我已阅读过文档并且它似乎可以工作,但我显然遗漏了一些东西。

location ~* /staging/(.*) {
    rewrite ^/staging/(.*)$ /staging/$1/index.php last;
}

答案1

也许您需要重新阅读该文档,因为它在这里被解释:

Syntax:   rewrite regex replacement [flag];
Default:  —
Context:  server, location, if

[...]

An optional flag parameter can be one of:

last 
   stops processing the current set of ngx_http_rewrite_module
   directives and starts a search for a new location matching the changed
   URI;

break
    stops processing the current set of ngx_http_rewrite_module
    directives as with the break directive;

redirect returns a temporary
    redirect with the 302 code; used if a replacement string does not
    start with “http://” or “https://”; permanent returns a permanent
    redirect with the 301 code.

因此,当您使用该last标志时,它就像是内部重定向到 nginx。我猜你声明了一个location ~ \.php$这个是为了处理 php 文件,但它不会被选入位置选举过程,因为第一个匹配的正则表达式获胜。因此它递归地进入前一个位置块。

答案2

尝试以下方法:

location ~* /staging/(.*) {
    rewrite ^/staging/(.*)/$ /staging/$1/index.php break;
}

或者限制到一个级别:

location ~* /staging/(*) {
    rewrite ^/staging/([^/]*)/$ /staging/$1/index.php break;
}

相关内容