【问题标题】:URL Rewrite in web.config of IIS to nginx.confIIS 的 web.config 中的 URL 重写为 nginx.conf
【发布时间】:2014-07-27 03:25:51
【问题描述】:

刚接触 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;
}

【问题讨论】:

  • 改用rewrite /?([^/]+)$
  • @hjpotter92 你是什么意思?尝试不工作,现在出现 500 个错误
  • location ~* /\.\w+$ { break; } 应该是if ($uri ~* \.\w+$) { break; }, break 表示它将停止在location / {} 中运行其他重写规则

标签: .htaccess iis nginx url-rewriting


【解决方案1】:

这适用于我的测试环境:

location / {
    if (-d $request_filename){
        rewrite ^/(.*[^/]+)/$ /_index/$1.html break;
    }

    location ~* /.*\.\w+$ {
        break;
    }

    rewrite ^/([^.]*[^/]+)$ /$1.html break;
}            

注意nginxrewrite directive:

可选的标志参数可以是以下之一:

最后

停止处理当前的 ngx_http_rewrite_module 指令集并开始搜索与更改的 URI 匹配的新位置;

打破

停止处理当前的 ngx_http_rewrite_module 指令集,就像使用 break 指令一样;

因此,如果您不希望在下一个循环中搜索重写的 URL 以匹配规则,则应使用 break

break directive:

停止处理当前的 ngx_http_rewrite_module 指令集。

如果在该位置内指定了指令,则在该位置继续对请求进行进一步处理。

这类似于URL Rewriter 的行为:

规则可能已打开 StopProcessing 标志。当此标志打开时,意味着将不再处理后续规则,并且此规则生成的 URL 将被传递到 IIS 请求管道(如果规则匹配)。默认情况下,此标志是关闭的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-23
    • 2011-05-23
    • 2016-11-03
    • 2013-04-23
    • 1970-01-01
    • 2014-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多