位置将所有 /*.xml 重定向到 /*

Location redirect all /*.xml to /*

我想将所有 /filename.xml 文件重定向到 /filename。

我可以像这样重定向单个文件

location = /mascus-export.xml {
     rewrite .* /mascus-export redirect;
}

但我不想单独列出每个 .xml 文件,而是希望全部重定向。像这样:

location = /*.xml {
     rewrite .* /* redirect;
}

我在每种组合中都尝试过这种类型的东西

location = /(*)$.xml {
     rewrite .* / redirect;
}

但似乎没有任何效果。

您正在尝试编写一个 正则表达式 location 块,它以 ~ 而不是 = 开头。有关详细信息,请参阅 this document

例如:

location ~ \.xml$ { ... }

rewrite 需要使用括号捕获部分 URI。例如:

rewrite ^(.*)\.xml$  redirect;

rewrite 可以很好地包含在上面的 location 块中,或者只是裸露在封闭的 server 块中。


如果要使用正则表达式 location 块,则不需要也使用 rewrite 语句。请改用 return 语句。例如:

location ~ ^(.*)\.xml$ { 
    return 302 ;
}