我正在使用 .NET Core 2.1。我试图将没有结尾斜杠的任何非文件 URL(不包含点)永久重定向到带有结尾斜杠的相应 URL。有没有办法使用 rewriteOptions 来完成此任务?
var rewriteOptions = new RewriteOptions()
.AddRedirect("^[^.]*$", "$1", 301)
.AddRedirectToHttpsPermanent();
Run Code Online (Sandbox Code Playgroud)
根据此文档添加尾部斜杠:
var options = new RewriteOptions()
.AddRedirect("(.*[^/])$", "$1/")
.AddRedirectToHttpsPermanent();
Run Code Online (Sandbox Code Playgroud)
为了防止静态文件发生这种情况,请确保在之前app.UseStaticFiles();调用 app.UseRewriter(options);
所以:
// Return static files and end the pipeline.
app.UseStaticFiles(); // <--- **before** the redirect
var options = new RewriteOptions()
.AddRedirect("(.*[^/])$", "$1/")
.AddRedirectToHttpsPermanent();
app.UseRewriter(options);
Run Code Online (Sandbox Code Playgroud)
首先调用 UseStaticFiles() 将缩短静态文件的管道。因此不会对静态文件进行重定向。
有关 Startup.Configure 订单的更多信息请参见此处:
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/middleware/?view=aspnetcore-2.1#order
| 归档时间: |
|
| 查看次数: |
6865 次 |
| 最近记录: |