在网址末尾添加斜杠

时间:2018-10-13 22:52:50

标签: asp.net-core

我正在使用.NET Core 2.1。我正在尝试将不带斜杠的所有非文件URL(不包含点)永久重定向到带斜杠的相应URL。有没有一种方法可以使用rewriteOptions完成?

        var rewriteOptions = new RewriteOptions()
            .AddRedirect("^[^.]*$", "$1", 301)
            .AddRedirectToHttpsPermanent();

3 个答案:

答案 0 :(得分:1)

根据this documentation添加斜杠为:

var options = new RewriteOptions()
        .AddRedirect("(.*[^/])$", "$1/")
        .AddRedirectToHttpsPermanent();

为防止它在静态文件中发生,请确保在{strong> 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);

首先调用UseStaticFiles()将缩短静态文件的管道。因此,不会对静态文件进行任何重定向。

有关启动的更多信息。请在此处配置订单

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/middleware/?view=aspnetcore-2.1#order

答案 1 :(得分:1)

模式应匹配:

/js                           # has no preceding segements
/www/js                       # has a segment  
/www/test.d/js                # contains a dot within preceding segments 

并且不应匹配:

/www/js/jquery.js             # contains a dot at last segment
/www/js/                      # has a trailing slash
/www/js/hello.d/jquery.js/    # has a trailing slash

因此,请创建以下模式:

var pattern = @"^(((.*/)|(/?))[^/.]+(?!/$))$"; 

var options = new RewriteOptions()
    .AddRedirect(pattern, "$1/",301);

它将起作用。

测试用例:

应该重定向:

GET https://localhost:5001/js HTTP/1.1
GET https://localhost:5001/xx/js HTTP/1.1
GET https://localhost:5001/xx/test.d/js HTTP/1.1

不应重定向:

GET https://localhost:5001/xx/js/ HTTP/1.1
GET https://localhost:5001/xx/jquery.js HTTP/1.1
GET https://localhost:5001/xx/hello.d/jquery.js HTTP/1.1

答案 2 :(得分:0)

为非文件 URL 添加尾部斜杠的方法是在 app.UseStaticFiles() 之后在 startup.cs 中配置中间件;

public class TrailingSlashUrlMiddleware
    {
        private readonly RequestDelegate _next;

        public TrailingSlashUrlMiddleware(RequestDelegate next)
        {
            _next = next;
        }

        public async Task Invoke(HttpContext httpContext)
        {
            string currentPath = httpContext.Request.Path;

            if (currentPath != "/")
            {
                

                string ext = System.IO.Path.GetExtension(currentPath);

                bool NeedRedirection = false;

                var currentQueryString = httpContext.Request.QueryString;

                if (string.IsNullOrEmpty(ext))
                {
                    if (!currentPath.EndsWith("/"))
                    {
                        currentPath = currentPath + "/";
                        NeedRedirection = true;
                    }

                    //if (currentQueryString.HasValue)
                    //{
                    //    string queryString = currentQueryString.Value;

                    //    if (!queryString.EndsWith("/"))
                    //    {
                    //        queryString = queryString + "/";
                    //        NeedRedirection = true;
                    //    }

                    //    currentPath += queryString;
                    //}
                }
                if (NeedRedirection)
                {
                    httpContext.Response.Redirect(currentPath, true);
                    return;
                }
            }

            await _next(httpContext);
        }
    }

如果您需要将此应用于查询字符串,请取消注释注释代码。