刚接触nginx,完全糊涂了。
如何将此URL重写为nginx?
<rewrite>
<rules>
<rule name="IndexFolders" stopProcessing="true">
<match url="(.*[^/]+)$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" />
</conditions>
<action type="Rewrite" url="/_index/{R:1}.html" />
</rule>
<rule name="OtherFile" stopProcessing="true">
<match url=".w+$" />
</rule>
<rule name="HtmlFile" stopProcessing="true">
<match url="^([^.]*[^/]+)$" />
<action type="Rewrite" url="{R:1}.html" />
</rule>
</rules>
</rewrite>
尝试转换,但不工作。
location / {
if (-d $request_filename){
set $rule_0 1;
}
if ($rule_0 = "1"){
rewrite /(.*[^/]+)$ /_index/$1.html last;
}
location ~* /.w+$ {
break;
}
rewrite ^/([^.]*[^/]+)$ /$1.html last;
}
这在我的测试环境中有效:
location / {
if (-d $request_filename){
rewrite ^/(.*[^/]+)/$ /_index/$1.html break;
}
location ~* /.*.w+$ {
break;
}
rewrite ^/([^.]*[^/]+)$ /$1.html break;
}
注意nginx重写指令:
可选的标志参数可以是:
最后停止处理当前的ngx_http_rewrite_module指令集,并开始搜索与修改后的URI匹配的新位置;
休息停止处理当前的ngx_http_rewrite_module指令集,就像break指令一样;
所以如果你不希望重写的URL在下一个循环中被搜索匹配规则,你应该使用break。
指令:
停止处理当前的ngx_http_rewrite_module指令集。
如果在location中指定了一个指令,请求的进一步处理将在这个location中继续进行。
这类似于URL重写器的行为:
规则可以打开StopProcessing标志。当这个标志被打开时,意味着不会再处理后续规则,并且该规则生成的URL将被传递到IIS请求管道(如果规则匹配)。默认情况下,该标志是关闭的。