ASP.NET Core 中的 URL 重写中间件

作者:Kirk LarkinRick Anderson

本文介绍了 URL 重写,并说明了如何在 ASP.NET Core 应用中使用 URL 重写中间件。

URL 重写是根据一个或多个预定义规则修改请求 URL 的行为。 URL 重写会在资源位置和地址之间创建一个抽象,使位置和地址不紧密相连。 在以下几种方案中,URL 重写很有价值:

  • 暂时或永久移动或替换服务器资源,并维护这些资源的稳定定位符。
  • 拆分在不同应用或同一应用的不同区域中处理的请求。
  • 删除、添加或重新组织传入请求上的 URL 段。
  • 优化搜索引擎优化 (SEO) 的公共 URL。
  • 允许使用友好的公共 URL 来帮助访问者预测请求资源后返回的内容。
  • 将不安全请求重定向到安全终结点。
  • 防止热链接,外部站点会通过热链接将其他站点的资产链接到其自己的内容,从而利用托管在其他站点上的静态资产。

URL 重写可能会降低应用的性能。 请限制规则的数量和复杂度。

URL 重定向和 URL 重写

URL 重定向和 URL 重写之间的用词差异很细微,但这对于向客户端提供资源具有重要意义 。 ASP.NET Core 的 URL 重写中间件能够满足两者的需求。

URL 重定向涉及客户端操作,指示客户端访问与客户端最初请求地址不同的资源。 这需要往返服务器。 客户端对资源发出新请求时,返回客户端的重定向 URL 会出现在浏览器地址栏。

如果 /resource 被重定向到 /different-resource,则服务器作出响应,指示客户端应在 /different-resource 获取资源,所提供的状态代码指示重定向是临时的还是永久的。

A WebAPI service endpoint has been temporarily changed from version 1 (v1) to version 2 (v2) on the server. A client makes a request to the service at the version 1 path /v1/api. The server sends back a 302 (Found) response with the new, temporary path for the service at version 2 /v2/api. The client makes a second request to the service at the redirect URL. The server responds with a 200 (OK) status code.

将请求重定向到不同 URL 时,通过使用响应指定状态代码来指示重定向是永久还是临时:

  • 如果资源有一个新的永久性 URL,并且所有将来的资源请求都应使用新的 URL,则使用 301 - Moved Permanently 状态代码。 收到 301 状态代码时,客户端可能会缓存响应并重用这段代码。

  • 如果重定向是临时的或一般情况下会更改的,则使用 302 - Found 状态代码。 302 状态代码向客户端指示不存储 URL 并在将来使用。

有关状态代码的详细信息,请参阅 RFC 9110:状态代码定义

URL 重写是服务器端操作,它从与客户端请求的资源地址不同的资源地址提供资源。 重写 URL 不需要往返服务器。 重写的 URL 不会返回客户端,也不会出现在浏览器地址栏。

如果 /resource 重写到 /different-resource,服务器会在内部提取并返回 /different-resource 处的资源 。

尽管客户端可能能够检索已重写 URL 处的资源,但是,客户端发出请求并收到响应时,并不知道已重写 URL 处存在的资源。

A WebAPI service endpoint has been changed from version 1 (v1) to version 2 (v2) on the server. A client makes a request to the service at the version 1 path /v1/api. The request URL is rewritten to access the service at the version 2 path /v2/api. The service responds to the client with a 200 (OK) status code.

URL 重写示例应用

通过示例应用了解 URL 重写中间件的功能。 该应用程序应用重定向和重写规则,并显示多个方案的重定向或重写的 URL。

何时使用 URL 重写中间件

当以下方法不能令人满意时,请使用 URL 重写中间件:

如果应用是托管在 HTTP.sys 服务器上,请使用 URL 重写中间件。

使用 IIS、Apache 和 Nginx 中的基于服务器的 URL 重写技术的主要原因:

  • 中间件不支持这些模块的完整功能。

    服务器模块的一些功能不适用于 ASP.NET Core 项目,例如 IIS 重写模块的 IsFileIsDirectory 约束。 在这些情况下,请改为使用中间件。

  • 中间件性能与模块性能不匹配。

    基准测试是能够确定地知道哪种方法对性能的降低影响最大,或者降低的性能是否可忽略不计的唯一方法。

扩展和选项

通过使用扩展方法为每条重写规则创建 RewriteOptions 类的一个实例,建立 URL 重写和重定向规则。 按应处理的顺序链接多个规则。 使用 UseRewriterRewriteOptions 添加到请求管道时,它会被传递到 URL 重写中间件:

using Microsoft.AspNetCore.Rewrite;
using RewriteRules;

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

using (StreamReader apacheModRewriteStreamReader =
    File.OpenText("ApacheModRewrite.txt"))
using (StreamReader iisUrlRewriteStreamReader =
    File.OpenText("IISUrlRewrite.xml"))
{
    var options = new RewriteOptions()
        .AddRedirect("redirect-rule/(.*)", "redirected/$1")
        .AddRewrite(@"^rewrite-rule/(\d+)/(\d+)", "rewritten?var1=$1&var2=$2",
            skipRemainingRules: true)
        .AddApacheModRewrite(apacheModRewriteStreamReader)
        .AddIISUrlRewrite(iisUrlRewriteStreamReader)
        .Add(MethodRules.RedirectXmlFileRequests)
        .Add(MethodRules.RewriteTextFileRequests)
        .Add(new RedirectImageRequests(".png", "/png-images"))
        .Add(new RedirectImageRequests(".jpg", "/jpg-images"));

    app.UseRewriter(options);
}

app.UseStaticFiles();

app.Run(context => context.Response.WriteAsync(
    $"Rewritten or Redirected Url: " +
    $"{context.Request.Path + context.Request.QueryString}"));

app.Run();

在上述代码中,MethodRules 是用户定义的类。 有关详细信息,请参阅本文中的 RewriteRules.cs

将非 www 重定向到 www

三个选项允许应用将非 www 重新定向到 www

URL 重定向

使用 AddRedirect 将请求重定向。 第一个参数包含用于匹配传入 URL 路径的 .NET 正则表达式 (Regex)。 第二个参数是替换字符串。 第三个参数(如有)指定状态代码。 如果没有指定状态代码,则状态代码默认为 302 - 已找到,指示资源暂时移动或替换。

using Microsoft.AspNetCore.Rewrite;
using RewriteRules;

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

using (StreamReader apacheModRewriteStreamReader =
    File.OpenText("ApacheModRewrite.txt"))
using (StreamReader iisUrlRewriteStreamReader =
    File.OpenText("IISUrlRewrite.xml"))
{
    var options = new RewriteOptions()
        .AddRedirect("redirect-rule/(.*)", "redirected/$1")
        .AddRewrite(@"^rewrite-rule/(\d+)/(\d+)", "rewritten?var1=$1&var2=$2",
            skipRemainingRules: true)
        .AddApacheModRewrite(apacheModRewriteStreamReader)
        .AddIISUrlRewrite(iisUrlRewriteStreamReader)
        .Add(MethodRules.RedirectXmlFileRequests)
        .Add(MethodRules.RewriteTextFileRequests)
        .Add(new RedirectImageRequests(".png", "/png-images"))
        .Add(new RedirectImageRequests(".jpg", "/jpg-images"));

    app.UseRewriter(options);
}

app.UseStaticFiles();

app.Run(context => context.Response.WriteAsync(
    $"Rewritten or Redirected Url: " +
    $"{context.Request.Path + context.Request.QueryString}"));

app.Run();

在启用了开发人员工具的浏览器中,向路径为 /redirect-rule/1234/5678 的示例应用发出请求。 正则表达式匹配 redirect-rule/(.*) 上的请求路径,且该路径会被 /redirected/1234/5678 替代。 重定向 URL 以“302 (已找到)”状态代码发回客户端。 浏览器会在浏览器地址栏中出现的重定向 URL 上发出新请求。 由于示例应用中的规则都不匹配重定向 URL:

  • 第二个请求从应用程序收到“200 (正常)”响应。
  • 响应正文显示了重定向 URL。

重定向 URL 时,系统将向服务器进行一次往返。

警告

建立重定向规则时务必小心。 系统会根据应用的每个请求(包括重定向后的请求)对重定向规则进行评估。 很容易便会意外创建无限重定向循环。

括号内的表达式部分称为“捕获组”。 表达式的点 (.) 表示匹配任何字符。 星号 (*) 表示零次或多次匹配前面的字符。 因此,URL 的最后两个路径段 1234/5678 由捕获组 (.*) 捕获。 在请求 URL 中提供的位于 redirect-rule/ 之后的任何值均由此单个捕获组捕获。

在替换字符串中,将捕获组注入带有美元符号 ($)、后跟捕获序列号的字符串中。 获取的第一个捕获组值为 $1,第二个为 $2,并且正则表达式中的其他捕获组值将依次继续排列。 redirect-rule/(.*) 的重定向规则正则表达式中只有一个捕获组,因此替换字符串中只有一个注入组,即 $1。 如果应用此规则,URL 将变为 /redirected/1234/5678

尝试在浏览器工具的“网络”选项卡上使用 /redirect-rule/1234/5678

URL 重定向到安全的终结点

使用 AddRedirectToHttps 将 HTTP 请求重定向到采用 HTTPS 协议的相同主机和路径。 如不提供状态代码,则中间件默认为“302(已找到)”。 如果不提供端口:

  • 中间件默认为 null
  • 方案更改为 https(HTTPS 协议),客户端访问端口 443 上的资源。

下面的示例展示了如何将状态代码设置为 301 - Moved Permanently,以及如何将端口更改为 Kestrel 在 localhost 上使用的 HTTPS 端口。 在生产环境中,HTTPS 端口设置为 null:

using Microsoft.AspNetCore.Rewrite;
using RewriteRules;

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

int? localhostHTTPSport = null;
if (app.Environment.IsDevelopment())
{
    localhostHTTPSport = Int32.Parse(Environment.GetEnvironmentVariable(
                   "ASPNETCORE_URLS")!.Split(new Char[] { ':', ';' })[2]);
}

using (StreamReader apacheModRewriteStreamReader =
    File.OpenText("ApacheModRewrite.txt"))
using (StreamReader iisUrlRewriteStreamReader =
    File.OpenText("IISUrlRewrite.xml"))
{
    var options = new RewriteOptions()
        // localhostHTTPport not needed for production, used only with localhost.
        .AddRedirectToHttps(301, localhostHTTPSport)
        .AddRedirect("redirect-rule/(.*)", "redirected/$1")
        .AddRewrite(@"^rewrite-rule/(\d+)/(\d+)", "rewritten?var1=$1&var2=$2",
            skipRemainingRules: true)
        .AddApacheModRewrite(apacheModRewriteStreamReader)
        .AddIISUrlRewrite(iisUrlRewriteStreamReader)
        .Add(MethodRules.RedirectXmlFileRequests)
        .Add(MethodRules.RewriteTextFileRequests)
        .Add(new RedirectImageRequests(".png", "/png-images"))
        .Add(new RedirectImageRequests(".jpg", "/jpg-images"));

    app.UseRewriter(options);
}

app.UseStaticFiles();

app.Run(context => context.Response.WriteAsync(
    $"Rewritten or Redirected Url: " +
    $"{context.Request.Path + context.Request.QueryString}"));

app.Run();

使用 AddRedirectToHttpsPermanent 将不安全的请求重定向到端口 443 上的采用安全 HTTPS 协议的相同主机和路径。 中间件将状态代码设置为 301 - Moved Permanently

using Microsoft.AspNetCore.Rewrite;
using RewriteRules;

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

using (StreamReader apacheModRewriteStreamReader =
    File.OpenText("ApacheModRewrite.txt"))
using (StreamReader iisUrlRewriteStreamReader =
    File.OpenText("IISUrlRewrite.xml"))
{
    var options = new RewriteOptions()
        .AddRedirectToHttpsPermanent()
        .AddRedirect("redirect-rule/(.*)", "redirected/$1")
        .AddRewrite(@"^rewrite-rule/(\d+)/(\d+)", "rewritten?var1=$1&var2=$2",
            skipRemainingRules: true)
        .AddApacheModRewrite(apacheModRewriteStreamReader)
        .AddIISUrlRewrite(iisUrlRewriteStreamReader)
        .Add(MethodRules.RedirectXmlFileRequests)
        .Add(MethodRules.RewriteTextFileRequests)
        .Add(new RedirectImageRequests(".png", "/png-images"))
        .Add(new RedirectImageRequests(".jpg", "/jpg-images"));

    app.UseRewriter(options);
}

app.UseStaticFiles();

app.Run(context => context.Response.WriteAsync(
    $"Rewritten or Redirected Url: " +
    $"{context.Request.Path + context.Request.QueryString}"));

app.Run();

注意

当重定向到安全的终结点并且不需要其他重定向规则时,建议使用 HTTPS 重定向中间件。 有关详细信息,请参阅强制使用 HTTPS

此示例应用演示了如何使用 AddRedirectToHttpsAddRedirectToHttpsPermanent。 在 http://redirect6.azurewebsites.net/iis-rules-rewrite/xyz 向应用发出不安全的 HTTP 请求。 在测试 localhost 上的 HTTP 到 HTTPS 重定向时:

  • 使用 HTTP URL,它具有与 HTTPS URL 不同的端口。 HTTP URL 位于 Properties/launchSettings.json 文件中。
  • https://localhost/{port} 删除 s 会失败,因为 localhost 不会对 HTTPS 端口做出 HTTP 响应。

下图显示了使用前面的代码请求 http://redirect6.azurewebsites.net/iis-rules-rewrite/xyz 的 F12 浏览器工具的图像:

Browser window with developer tools tracking the requests and responses: Add redirect to HTTPS

URL 重写

使用 AddRewrite 创建重写 URL 的规则。 第一个参数包含用于匹配传入 URL 路径的正则表达式。 第二个参数是替换字符串。 第三个参数 skipRemainingRules: {true|false} 指示如果当前规则适用,中间件是否要跳过其他重写规则。

using Microsoft.AspNetCore.Rewrite;
using RewriteRules;

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

using (StreamReader apacheModRewriteStreamReader =
    File.OpenText("ApacheModRewrite.txt"))
using (StreamReader iisUrlRewriteStreamReader =
    File.OpenText("IISUrlRewrite.xml"))
{
    var options = new RewriteOptions()
        .AddRedirectToHttpsPermanent()
        .AddRedirect("redirect-rule/(.*)", "redirected/$1")
        .AddRewrite(@"^rewrite-rule/(\d+)/(\d+)", "rewritten?var1=$1&var2=$2",
            skipRemainingRules: true)
        .AddApacheModRewrite(apacheModRewriteStreamReader)
        .AddIISUrlRewrite(iisUrlRewriteStreamReader)
        .Add(MethodRules.RedirectXmlFileRequests)
        .Add(MethodRules.RewriteTextFileRequests)
        .Add(new RedirectImageRequests(".png", "/png-images"))
        .Add(new RedirectImageRequests(".jpg", "/jpg-images"));

    app.UseRewriter(options);
}

app.UseStaticFiles();

app.Run(context => context.Response.WriteAsync(
    $"Rewritten or Redirected Url: " +
    $"{context.Request.Path + context.Request.QueryString}"));

app.Run();

尝试将请求发送到 https://redirect6.azurewebsites.net/rewrite-rule/1234/5678

表达式开头的脱字号 (^) 意味着匹配从 URL 路径的开头处开始。

在前面的重定向规则 redirect-rule/(.*) 的示例中,正则表达式的开头没有脱字号 (^)。 因此,路径中 redirect-rule/ 之前的任何字符都可能成功匹配。

路径 匹配
/redirect-rule/1234/5678
/my-cool-redirect-rule/1234/5678
/anotherredirect-rule/1234/5678

重写规则 ^rewrite-rule/(\d+)/(\d+) 只能与以 rewrite-rule/ 开头的路径匹配。 注意下表中的匹配差异。

路径 匹配
/rewrite-rule/1234/5678
/my-cool-rewrite-rule/1234/5678
/anotherrewrite-rule/1234/5678

在表达式的 ^rewrite-rule/ 部分之后,有两个捕获组 (\d+)/(\d+)\d 表示与数字匹配。 加号 (+) 表示与前面的一个或多个字符匹配。 因此,URL 必须包含数字加正斜杠加另一个数字的形式。 这些捕获组以 $1$2 的形式注入重写 URL 中。 重写规则替换字符串将捕获组放入查询字符串中。 重写 /rewrite-rule/1234/5678 的请求路径,返回 /rewritten?var1=1234&var2=5678 处的资源。 如果原始请求中存在查询字符串,则重写 URL 时会保留此字符串。

无需往返服务器来返回资源。 如果资源存在,系统会提取资源并以“200(正常)”状态代码返回给客户端。 因为客户端不会被重定向,所以浏览器地址栏中的 URL 不会发生更改。 客户端无法检测到服务器上发生的 URL 重写操作。

URL 重写和重定向的性能提示

对于最快的响应:

  • 按照从最频繁匹配的规则到最不频繁匹配的规则排列重写规则。
  • 尽可能使用 skipRemainingRules: true,因为匹配规则在计算上很昂贵并且增加了应用响应时间。 如果出现匹配项且无需处理任何其他规则,则跳过剩余规则的处理。

警告

恶意用户可能会向 RegularExpressions 提供昂贵的处理输入,从而导致拒绝服务攻击。 使用 RegularExpressions 的 ASP.NET Core 框架 API 会传递一个超时。 例如,RedirectRuleRewriteRule 类都会传递一个一秒的超时。

Apache mod_rewrite

使用 AddApacheModRewrite 应用 Apache mod_rewrite 规则。 请确保将规则文件与应用一起部署。 有关 mod_rewrite 规则的详细信息和示例,请参阅 Apache mod_rewrite

StreamReader 用于读取 ApacheModRewrite.txt 规则文件中的规则:

using Microsoft.AspNetCore.Rewrite;
using RewriteRules;

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

using (StreamReader apacheModRewriteStreamReader =
    File.OpenText("ApacheModRewrite.txt"))
using (StreamReader iisUrlRewriteStreamReader =
    File.OpenText("IISUrlRewrite.xml"))
{
    var options = new RewriteOptions()
        .AddRedirectToHttpsPermanent()
        .AddRedirect("redirect-rule/(.*)", "redirected/$1")
        .AddRewrite(@"^rewrite-rule/(\d+)/(\d+)", "rewritten?var1=$1&var2=$2",
            skipRemainingRules: true)
        .AddApacheModRewrite(apacheModRewriteStreamReader)
        .AddIISUrlRewrite(iisUrlRewriteStreamReader)
        .Add(MethodRules.RedirectXmlFileRequests)
        .Add(MethodRules.RewriteTextFileRequests)
        .Add(new RedirectImageRequests(".png", "/png-images"))
        .Add(new RedirectImageRequests(".jpg", "/jpg-images"));

    app.UseRewriter(options);
}

app.UseStaticFiles();

app.Run(context => context.Response.WriteAsync(
    $"Rewritten or Redirected Url: " +
    $"{context.Request.Path + context.Request.QueryString}"));

app.Run();

示例应用将请求从 /apache-mod-rules-redirect/(.\*) 重定向到 /redirected?id=$1。 响应状态代码为“302 (已找到)”。

# Rewrite path with additional sub directory
RewriteRule ^/apache-mod-rules-redirect/(.*) /redirected?id=$1 [L,R=302]

尝试将请求发送到 https://redirect6.azurewebsites.net/apache-mod-rules-redirect/1234

Apache 中间件支持下列 Apache mod_rewrite 服务器变量:

  • CONN_REMOTE_ADDR
  • HTTP_ACCEPT
  • HTTP_CONNECTION
  • HTTP_COOKIE
  • HTTP_FORWARDED
  • HTTP_HOST
  • HTTP_REFERER
  • HTTP_USER_AGENT
  • HTTPS
  • IPV6
  • QUERY_STRING
  • REMOTE_ADDR
  • REMOTE_PORT
  • REQUEST_FILENAME
  • REQUEST_METHOD
  • REQUEST_SCHEME
  • REQUEST_URI
  • SCRIPT_FILENAME
  • SERVER_ADDR
  • SERVER_PORT
  • SERVER_PROTOCOL
  • TIME
  • TIME_DAY
  • TIME_HOUR
  • TIME_MIN
  • TIME_MON
  • TIME_SEC
  • TIME_WDAY
  • TIME_YEAR

IIS URL 重写模块规则

若要使用适用于 IIS URL 重写模块的同一规则集,使用 AddIISUrlRewrite。 请确保将规则文件与应用一起部署。 当在 Windows Server IIS 上运行时,请勿指示中间件使用应用的 web.config 文件。 使用 IIS 时,应将这些规则存储在应用的 web.config 文件之外,以避免与 IIS 重写模块发生冲突。 有关 IIS URL 重写模块规则的详细信息和示例,请参阅 Using Url Rewrite Module 2.0(使用 URL 重写模块 2.0)和 URL Rewrite Module Configuration Reference(URL 重写模块配置引用)。

StreamReader 用于读取 IISUrlRewrite.xml 规则文件中的规则:

using Microsoft.AspNetCore.Rewrite;
using RewriteRules;

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

using (StreamReader apacheModRewriteStreamReader =
    File.OpenText("ApacheModRewrite.txt"))
using (StreamReader iisUrlRewriteStreamReader =
    File.OpenText("IISUrlRewrite.xml"))
{
    var options = new RewriteOptions()
        .AddRedirectToHttpsPermanent()
        .AddRedirect("redirect-rule/(.*)", "redirected/$1")
        .AddRewrite(@"^rewrite-rule/(\d+)/(\d+)", "rewritten?var1=$1&var2=$2",
            skipRemainingRules: true)
        .AddApacheModRewrite(apacheModRewriteStreamReader)
        .AddIISUrlRewrite(iisUrlRewriteStreamReader)
        .Add(MethodRules.RedirectXmlFileRequests)
        .Add(MethodRules.RewriteTextFileRequests)
        .Add(new RedirectImageRequests(".png", "/png-images"))
        .Add(new RedirectImageRequests(".jpg", "/jpg-images"));

    app.UseRewriter(options);
}

app.UseStaticFiles();

app.Run(context => context.Response.WriteAsync(
    $"Rewritten or Redirected Url: " +
    $"{context.Request.Path + context.Request.QueryString}"));

app.Run();

示例应用将请求从 /iis-rules-rewrite/(.*) 重写为 /rewritten?id=$1。 以“200 (正常)”状态代码作为响应发送到客户端。

<rewrite>
  <rules>
    <rule name="Rewrite segment to id querystring" stopProcessing="true">
      <match url="^iis-rules-rewrite/(.*)$" />
      <action type="Rewrite" url="rewritten?id={R:1}" appendQueryString="false"/>
    </rule>
  </rules>
</rewrite>

尝试将请求发送到 https://redirect6.azurewebsites.net/iis-rules-rewrite/xyz

如果应用具有配置了服务器级别规则的活动 IIS 重写模块,会对应用产生不利影响:

  • 请考虑禁用应用的 IIS 重写模块。
  • 有关详细信息,请参阅禁用 IIS 模块

不支持的功能

中间件不支持以下 IIS URL 重写模块功能:

  • 出站规则
  • 自定义服务器变量
  • 通配符
  • LogRewrittenUrl

受支持的服务器变量

中间件支持下列 IIS URL 重写模块服务器变量:

  • CONTENT_LENGTH
  • CONTENT_TYPE
  • HTTP_ACCEPT
  • HTTP_CONNECTION
  • HTTP_COOKIE
  • HTTP_HOST
  • HTTP_REFERER
  • HTTP_URL
  • HTTP_USER_AGENT
  • HTTPS
  • LOCAL_ADDR
  • QUERY_STRING
  • REMOTE_ADDR
  • REMOTE_PORT
  • REQUEST_FILENAME
  • REQUEST_URI

IFileProvider 可通过 PhysicalFileProvider 获取。 这种方法可为重写规则文件的位置提供更大的灵活性。 请确保将重写规则文件部署到所提供路径的服务器中。

var fileProvider = new PhysicalFileProvider(Directory.GetCurrentDirectory());

基于方法的规则

使用 Add 在方法中实现自定义规则逻辑。 Add 公开 RewriteContext,这使 HttpContext 可用于重定向方法。 RewriteContext.Result 属性决定了如何处理其他管道进程。 将值设置为下表中的 RuleResult 字段之一。

重写上下文结果 操作
RuleResult.ContinueRules(默认值) 继续应用规则。
RuleResult.EndResponse 停止应用规则并发送响应。
RuleResult.SkipRemainingRules 停止应用规则并将上下文发送给下一个中间件。
using Microsoft.AspNetCore.Rewrite;
using RewriteRules;

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

using (StreamReader apacheModRewriteStreamReader =
    File.OpenText("ApacheModRewrite.txt"))
using (StreamReader iisUrlRewriteStreamReader =
    File.OpenText("IISUrlRewrite.xml"))
{
    var options = new RewriteOptions()
        .AddRedirectToHttpsPermanent()
        .AddRedirect("redirect-rule/(.*)", "redirected/$1")
        .AddRewrite(@"^rewrite-rule/(\d+)/(\d+)", "rewritten?var1=$1&var2=$2",
            skipRemainingRules: true)
        .AddApacheModRewrite(apacheModRewriteStreamReader)
        .AddIISUrlRewrite(iisUrlRewriteStreamReader)
        .Add(MethodRules.RedirectXmlFileRequests)
        .Add(MethodRules.RewriteTextFileRequests)
        .Add(new RedirectImageRequests(".png", "/png-images"))
        .Add(new RedirectImageRequests(".jpg", "/jpg-images"));

    app.UseRewriter(options);
}

app.UseStaticFiles();

app.Run(context => context.Response.WriteAsync(
    $"Rewritten or Redirected Url: " +
    $"{context.Request.Path + context.Request.QueryString}"));

app.Run();

示例应用演示了如何对以 .xml 结尾的路径的请求进行重定向。 向 /file.xml 发出请求时:

  • 请求被重定向到 /xmlfiles/file.xml
  • 状态代码设置为 301 - Moved Permanently。 当浏览器发出针对 /xmlfiles/file.xml 的新请求后,静态文件中间件会将文件从 wwwroot/xmlfiles 文件夹提供给客户端。 对于重定向,请显式设置响应的状态代码。 否则,将会返回“200 (正常)”状态代码,且客户端上不会发生重写。

RewriteRules.cs

public static void RedirectXmlFileRequests(RewriteContext context)
{
    var request = context.HttpContext.Request;

    // Because the client is redirecting back to the same app, stop 
    // processing if the request has already been redirected.
    if (request.Path.StartsWithSegments(new PathString("/xmlfiles")) ||
        request.Path.Value==null)
    {
        return;
    }

    if (request.Path.Value.EndsWith(".xml", StringComparison.OrdinalIgnoreCase))
    {
        var response = context.HttpContext.Response;
        response.StatusCode = (int) HttpStatusCode.MovedPermanently;
        context.Result = RuleResult.EndResponse;
        response.Headers[HeaderNames.Location] = 
            "/xmlfiles" + request.Path + request.QueryString;
    }
}

此方法还可以重写请求。 示例应用演示了如何重写任何文本文件请求的路径以从 wwwroot 文件夹中提供 file.txt 文本文件 。 静态文件中间件基于更新的请求路径来提供文件:

using Microsoft.AspNetCore.Rewrite;
using RewriteRules;

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

using (StreamReader apacheModRewriteStreamReader =
    File.OpenText("ApacheModRewrite.txt"))
using (StreamReader iisUrlRewriteStreamReader =
    File.OpenText("IISUrlRewrite.xml"))
{
    var options = new RewriteOptions()
        .AddRedirectToHttpsPermanent()
        .AddRedirect("redirect-rule/(.*)", "redirected/$1")
        .AddRewrite(@"^rewrite-rule/(\d+)/(\d+)", "rewritten?var1=$1&var2=$2",
            skipRemainingRules: true)
        .AddApacheModRewrite(apacheModRewriteStreamReader)
        .AddIISUrlRewrite(iisUrlRewriteStreamReader)
        .Add(MethodRules.RedirectXmlFileRequests)
        .Add(MethodRules.RewriteTextFileRequests)
        .Add(new RedirectImageRequests(".png", "/png-images"))
        .Add(new RedirectImageRequests(".jpg", "/jpg-images"));

    app.UseRewriter(options);
}

app.UseStaticFiles();

app.Run(context => context.Response.WriteAsync(
    $"Rewritten or Redirected Url: " +
    $"{context.Request.Path + context.Request.QueryString}"));

app.Run();

RewriteRules.cs

public static void RewriteTextFileRequests(RewriteContext context)
{
    var request = context.HttpContext.Request;

    if (request.Path.Value != null &&
        request.Path.Value.EndsWith(".txt", StringComparison.OrdinalIgnoreCase))
    {
        context.Result = RuleResult.SkipRemainingRules;
        request.Path = "/file.txt";
    }
}

基于 IRule 的规则

使用 Add 在实现 IRule 接口的类中使用规则逻辑。 与使用基于方法的规则方法相比,IRule 提供了更大的灵活性。 实现类可能包含构造函数,可在其中传入 ApplyRule 方法的参数。

using Microsoft.AspNetCore.Rewrite;
using RewriteRules;

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

using (StreamReader apacheModRewriteStreamReader =
    File.OpenText("ApacheModRewrite.txt"))
using (StreamReader iisUrlRewriteStreamReader =
    File.OpenText("IISUrlRewrite.xml"))
{
    var options = new RewriteOptions()
        .AddRedirectToHttpsPermanent()
        .AddRedirect("redirect-rule/(.*)", "redirected/$1")
        .AddRewrite(@"^rewrite-rule/(\d+)/(\d+)", "rewritten?var1=$1&var2=$2",
            skipRemainingRules: true)
        .AddApacheModRewrite(apacheModRewriteStreamReader)
        .AddIISUrlRewrite(iisUrlRewriteStreamReader)
        .Add(MethodRules.RedirectXmlFileRequests)
        .Add(MethodRules.RewriteTextFileRequests)
        .Add(new RedirectImageRequests(".png", "/png-images"))
        .Add(new RedirectImageRequests(".jpg", "/jpg-images"));

    app.UseRewriter(options);
}

app.UseStaticFiles();

app.Run(context => context.Response.WriteAsync(
    $"Rewritten or Redirected Url: " +
    $"{context.Request.Path + context.Request.QueryString}"));

app.Run();

检查示例应用中 extensionnewPath 的参数值是否符合多个条件。 extension 必须包含一个值,并且该值必须是 .png.jpg.gif。 如果 newPath 无效,则会引发 ArgumentException。 如果发出针对 image.png 的请求,请求将重定向到 /png-images/image.png。 如果发出针对 image.jpg 的请求,请求将重定向到 /jpg-images/image.jpg。 状态代码设置为 301 - Moved Permanentlycontext.Result 设置为停止处理规则并发送响应。

public class RedirectImageRequests : IRule
{
    private readonly string _extension;
    private readonly PathString _newPath;

    public RedirectImageRequests(string extension, string newPath)
    {
        if (string.IsNullOrEmpty(extension))
        {
            throw new ArgumentException(nameof(extension));
        }

        if (!Regex.IsMatch(extension, @"^\.(png|jpg|gif)$"))
        {
            throw new ArgumentException("Invalid extension", nameof(extension));
        }

        if (!Regex.IsMatch(newPath, @"(/[A-Za-z0-9]+)+?"))
        {
            throw new ArgumentException("Invalid path", nameof(newPath));
        }

        _extension = extension;
        _newPath = new PathString(newPath);
    }

    public void ApplyRule(RewriteContext context)
    {
        var request = context.HttpContext.Request;

        // Because we're redirecting back to the same app, stop 
        // processing if the request has already been redirected
        if (request.Path.StartsWithSegments(new PathString(_newPath)) ||
            request.Path.Value == null)
        {
            return;
        }

        if (request.Path.Value.EndsWith(_extension, StringComparison.OrdinalIgnoreCase))
        {
            var response = context.HttpContext.Response;
            response.StatusCode = (int) HttpStatusCode.MovedPermanently;
            context.Result = RuleResult.EndResponse;
            response.Headers[HeaderNames.Location] = 
                _newPath + request.Path + request.QueryString;
        }
    }
}

尝试:

  • PNG 请求:https://redirect6.azurewebsites.net/image.png
  • JPG 请求:https://redirect6.azurewebsites.net/image.jpg

正则表达式示例

目标 正则表达式字符串和
匹配示例
替换字符串和
输出示例
将路径重写为查询字符串 ^path/(.*)/(.*)
/path/abc/123
path?var1=$1&var2=$2
/path?var1=abc&var2=123
去掉尾部反斜杠 ^path2/(.*)/$
/path2/xyz/
$1
/path2/xyz
强制添加尾部反斜杠 ^path3/(.*[^/])$
/path3/xyz
$1/
/path3/xyz/
避免重写特定请求 ^(.*)(?<!\.axd)$
^(?!.*\.axd$)(.*)$
正确:/path4/resource.htm
错误:/path4/resource.axd
rewritten/$1
/rewritten/resource.htm
/resource.axd
重新排列 URL 段 path5/(.*)/(.*)/(.*)
path5/1/2/3
path5/$3/$2/$1
path5/3/2/1
替换 URL 段 ^path6/(.*)/segment2/(.*)
^path6/segment1/segment2/segment3
path6/$1/replaced/$2
/path6/segment1/replaced/segment3

上表中的链接使用部署到 Azure 的以下代码:

using Microsoft.AspNetCore.Rewrite;
using RewriteRules;

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

using (StreamReader apacheModRewriteStreamReader =
    File.OpenText("ApacheModRewrite.txt"))
using (StreamReader iisUrlRewriteStreamReader =
    File.OpenText("IISUrlRewrite.xml"))
{
    var options = new RewriteOptions()
        .AddRedirectToHttpsPermanent()
        .AddRedirect("redirect-rule/(.*)", "redirected/$1")
        .AddRewrite(@"^rewrite-rule/(\d+)/(\d+)", "rewritten?var1=$1&var2=$2",
            skipRemainingRules: true)

        // Rewrite path to QS.
        .AddRewrite(@"^path/(.*)/(.*)", "path?var1=$1&var2=$2",
            skipRemainingRules: true)
        // Skip trailing slash.
        .AddRewrite(@"^path2/(.*)/$", "path2/$1",
            skipRemainingRules: true)
         // Enforce trailing slash.
         .AddRewrite(@"^path3/(.*[^/])$", "path3/$1/",
            skipRemainingRules: true)
         // Avoid rewriting specific requests.
         .AddRewrite(@"^path4/(.*)(?<!\.axd)$", "rewritten/$1",
            skipRemainingRules: true)
         // Rearrange URL segments
         .AddRewrite(@"^path5/(.*)/(.*)/(.*)", "path5/$3/$2/$1",
            skipRemainingRules: true)
          // Replace a URL segment
          .AddRewrite(@"^path6/(.*)/segment2/(.*)", "path6/$1/replaced/$2",
            skipRemainingRules: true)

        .AddApacheModRewrite(apacheModRewriteStreamReader)
        .AddIISUrlRewrite(iisUrlRewriteStreamReader)
        .Add(MethodRules.RedirectXmlFileRequests)
        .Add(MethodRules.RewriteTextFileRequests)
        .Add(new RedirectImageRequests(".png", "/png-images"))
        .Add(new RedirectImageRequests(".jpg", "/jpg-images"));

    app.UseRewriter(options);
}

app.UseStaticFiles();

app.Run(context => context.Response.WriteAsync(
    $"Rewritten or Redirected Url: " +
    $"{context.Request.Path + context.Request.QueryString}"));

app.Run();

在前面的大多数正则表达式示例中,文本 path 用于为部署的示例提供独特的可测试重写规则。 通常,正则表达式不包括 path。 例如,请参阅下面的正则表达式示例表。

本文档介绍 URL 重写并说明如何在 ASP.NET Core 应用中使用 URL 重写中间件。

URL 重写是根据一个或多个预定义规则修改请求 URL 的行为。 URL 重写会在资源位置和地址之间创建一个抽象,使位置和地址不紧密相连。 在以下几种方案中,URL 重写很有价值:

  • 暂时或永久移动或替换服务器资源,并维护这些资源的稳定定位符。
  • 拆分在不同应用或同一应用的不同区域中处理的请求。
  • 删除、添加或重新组织传入请求上的 URL 段。
  • 优化搜索引擎优化 (SEO) 的公共 URL。
  • 允许使用友好的公共 URL 来帮助访问者预测请求资源后返回的内容。
  • 将不安全请求重定向到安全终结点。
  • 防止热链接,外部站点会通过热链接将其他站点的资产链接到其自己的内容,从而利用托管在其他站点上的静态资产。

注意

URL 重写可能会降低应用的性能。 如果可行,应限制规则的数量和复杂度。

查看或下载示例代码如何下载

URL 重定向和 URL 重写

URL 重定向和 URL 重写之间的用词差异很细微,但这对于向客户端提供资源具有重要意义 。 ASP.NET Core 的 URL 重写中间件能够满足两者的需求。

URL 重定向涉及客户端操作,指示客户端访问与客户端最初请求地址不同的资源。 这需要往返服务器。 客户端对资源发出新请求时,返回客户端的重定向 URL 会出现在浏览器地址栏。

如果 /resource 被重定向到 /different-resource,则服务器作出响应,指示客户端应在 /different-resource 获取资源,所提供的状态代码指示重定向是临时的还是永久的。

A WebAPI service endpoint has been temporarily changed from version 1 (v1) to version 2 (v2) on the server. A client makes a request to the service at the version 1 path /v1/api. The server sends back a 302 (Found) response with the new, temporary path for the service at version 2 /v2/api. The client makes a second request to the service at the redirect URL. The server responds with a 200 (OK) status code.

将请求重定向到不同 URL 时,通过使用响应指定状态代码来指示重定向是永久还是临时:

  • 如果资源有一个新的永久性 URL,并且你希望指示客户端所有将来的资源请求都使用新 URL,则使用 301 - Moved Permanently 状态代码。 收到 301 状态代码时,客户端可能会缓存响应并重用这段代码。

  • “302 (找到)”状态代码用于后列情况:重定向操作是临时的或通常会发生变化。 302 状态代码向客户端指示不存储 URL 并在将来使用。

有关状态代码的详细信息,请参阅 RFC 9110:状态代码定义

URL 重写是服务器端操作,它从与客户端请求的资源地址不同的资源地址提供资源。 重写 URL 不需要往返服务器。 重写的 URL 不会返回客户端,也不会出现在浏览器地址栏。

如果 /resource 重写到 /different-resource,服务器会在内部提取并返回 /different-resource 处的资源 。

尽管客户端可能能够检索已重写 URL 处的资源,但是,客户端发出请求并收到响应时,并不知道已重写 URL 处存在的资源。

A WebAPI service endpoint has been changed from version 1 (v1) to version 2 (v2) on the server. A client makes a request to the service at the version 1 path /v1/api. The request URL is rewritten to access the service at the version 2 path /v2/api. The service responds to the client with a 200 (OK) status code.

URL 重写示例应用

可使用示例应用了解 URL 重写中间件的功能。 该应用程序应用重定向和重写规则,并显示多个方案的重定向或重写的 URL。

何时使用 URL 重写中间件

如果无法使用以下方法,请使用 URL 重写中间件:

如果应用是托管在 HTTP.sys 服务器上,请使用 URL 重写中间件。

使用 IIS、Apache 和 Nginx 中的基于服务器的 URL 重写技术的主要原因:

  • 中间件不支持这些模块的完整功能。

    服务器模块的一些功能不适用于 ASP.NET Core 项目,例如 IIS 重写模块的 IsFileIsDirectory 约束。 在这些情况下,请改为使用中间件。

  • 中间件性能与模块性能不匹配。

    基准测试是确定哪种方法会最大程度降低性能或降低的性能是否可忽略不计的唯一方法。

Package

URL 重写中间件由 Microsoft.AspNetCore.Rewrite 包提供,该包隐式包含在 ASP.NET Core 应用中。

扩展和选项

通过使用扩展方法为每条重写规则创建 RewriteOptions 类的实例,建立 URL 重写和重写定向规则。 按所需的处理顺序链接多个规则。 使用 UseRewriterRewriteOptions 添加到请求管道时,它会被传递到 URL 重写中间件:

public void Configure(IApplicationBuilder app)
{
    using (StreamReader apacheModRewriteStreamReader = 
        File.OpenText("ApacheModRewrite.txt"))
    using (StreamReader iisUrlRewriteStreamReader = 
        File.OpenText("IISUrlRewrite.xml")) 
    {
        var options = new RewriteOptions()
            .AddRedirect("redirect-rule/(.*)", "redirected/$1")
            .AddRewrite(@"^rewrite-rule/(\d+)/(\d+)", "rewritten?var1=$1&var2=$2", 
                skipRemainingRules: true)
            .AddApacheModRewrite(apacheModRewriteStreamReader)
            .AddIISUrlRewrite(iisUrlRewriteStreamReader)
            .Add(MethodRules.RedirectXmlFileRequests)
            .Add(MethodRules.RewriteTextFileRequests)
            .Add(new RedirectImageRequests(".png", "/png-images"))
            .Add(new RedirectImageRequests(".jpg", "/jpg-images"));

        app.UseRewriter(options);
    }

    app.UseStaticFiles();

    app.Run(context => context.Response.WriteAsync(
        $"Rewritten or Redirected Url: " +
        $"{context.Request.Path + context.Request.QueryString}"));
}

将非 www 重定向到 www

三个选项允许应用将非 www 重新定向到 www

URL 重定向

使用 AddRedirect 将请求重定向。 第一个参数包含用于匹配传入 URL 路径的正则表达式。 第二个参数是替换字符串。 第三个参数(如有)指定状态代码。 如不指定状态代码,则状态代码默认为“302 (已找到)”,指示资源暂时移动或替换。

public void Configure(IApplicationBuilder app)
{
    using (StreamReader apacheModRewriteStreamReader = 
        File.OpenText("ApacheModRewrite.txt"))
    using (StreamReader iisUrlRewriteStreamReader = 
        File.OpenText("IISUrlRewrite.xml")) 
    {
        var options = new RewriteOptions()
            .AddRedirect("redirect-rule/(.*)", "redirected/$1")
            .AddRewrite(@"^rewrite-rule/(\d+)/(\d+)", "rewritten?var1=$1&var2=$2", 
                skipRemainingRules: true)
            .AddApacheModRewrite(apacheModRewriteStreamReader)
            .AddIISUrlRewrite(iisUrlRewriteStreamReader)
            .Add(MethodRules.RedirectXmlFileRequests)
            .Add(MethodRules.RewriteTextFileRequests)
            .Add(new RedirectImageRequests(".png", "/png-images"))
            .Add(new RedirectImageRequests(".jpg", "/jpg-images"));

        app.UseRewriter(options);
    }

    app.UseStaticFiles();

    app.Run(context => context.Response.WriteAsync(
        $"Rewritten or Redirected Url: " +
        $"{context.Request.Path + context.Request.QueryString}"));
}

在启用了开发人员工具的浏览器中,向路径为 /redirect-rule/1234/5678 的示例应用发出请求。 正则表达式匹配 redirect-rule/(.*) 上的请求路径,且该路径会被 /redirected/1234/5678 替代。 重定向 URL 以“302 (已找到)”状态代码发回客户端。 浏览器会在浏览器地址栏中出现的重定向 URL 上发出新请求。 由于示例应用中的规则都不匹配重定向 URL:

  • 第二个请求从应用程序收到“200 (正常)”响应。
  • 响应正文显示了重定向 URL。

重定向 URL 时,系统将向服务器进行一次往返。

警告

建立重定向规则时务必小心。 系统会根据应用的每个请求(包括重定向后的请求)对重定向规则进行评估。 很容易便会意外创建无限重定向循环。

原始请求:/redirect-rule/1234/5678

Browser window with developer tools tracking the requests and responses: Add redirect

括号内的表达式部分称为“捕获组”。 表达式的点 (.) 表示匹配任何字符。 星号 (*) 表示零次或多次匹配前面的字符。 因此,URL 的最后两个路径段 1234/5678 由捕获组 (.*) 捕获。 在请求 URL 中提供的位于 redirect-rule/ 之后的任何值均由此单个捕获组捕获。

在替换字符串中,将捕获组注入带有美元符号 ($)、后跟捕获序列号的字符串中。 获取的第一个捕获组值为 $1,第二个为 $2,并且正则表达式中的其他捕获组值将依次继续排列。 示例应用的重定向规则正则表达式中只有一个捕获组,因此替换字符串中只有一个注入组,即 $1。 如果应用此规则,URL 将变为 /redirected/1234/5678

URL 重定向到安全的终结点

使用 AddRedirectToHttps 将 HTTP 请求重定向到采用 HTTPS 协议的相同主机和路径。 如不提供状态代码,则中间件默认为“302(已找到)”。 如果不提供端口:

  • 中间件默认为 null
  • 方案更改为 https(HTTPS 协议),客户端访问端口 443 上的资源。

下面的示例展示了如何将状态代码设置为 301 - Moved Permanently 并将端口更改为 5001。

public void Configure(IApplicationBuilder app)
{
    var options = new RewriteOptions()
        .AddRedirectToHttps(301, 5001);

    app.UseRewriter(options);
}

使用 AddRedirectToHttpsPermanent 将不安全的请求重定向到端口 443 上的采用安全 HTTPS 协议的相同主机和路径。 中间件将状态代码设置为 301 - Moved Permanently

public void Configure(IApplicationBuilder app)
{
    var options = new RewriteOptions()
        .AddRedirectToHttpsPermanent();

    app.UseRewriter(options);
}

注意

当重定向到安全的终结点并且不需要其他重定向规则时,建议使用 HTTPS 重定向中间件。 有关详细信息,请参阅强制使用 HTTPS主题。

示例应用能够演示如何使用 AddRedirectToHttpsAddRedirectToHttpsPermanent。 将扩展方法添加到 RewriteOptions。 在任何 URL 上向应用发出不安全的请求。 消除自签名证书不受信任的浏览器安全警告,或创建例外以信任证书。

使用 AddRedirectToHttps(301, 5001) 的原始请求:http://localhost:5000/secure

Browser window with developer tools tracking the requests and responses: Add redirect to HTTPS

使用 AddRedirectToHttpsPermanent 的原始请求:http://localhost:5000/secure

Browser window with developer tools tracking the requests and responses: Add redirect to HTTPS permanent

URL 重写

使用 AddRewrite 创建重写 URL 的规则。 第一个参数包含用于匹配传入 URL 路径的正则表达式。 第二个参数是替换字符串。 第三个参数 skipRemainingRules: {true|false} 指示如果当前规则适用,中间件是否要跳过其他重写规则。

public void Configure(IApplicationBuilder app)
{
    using (StreamReader apacheModRewriteStreamReader = 
        File.OpenText("ApacheModRewrite.txt"))
    using (StreamReader iisUrlRewriteStreamReader = 
        File.OpenText("IISUrlRewrite.xml")) 
    {
        var options = new RewriteOptions()
            .AddRedirect("redirect-rule/(.*)", "redirected/$1")
            .AddRewrite(@"^rewrite-rule/(\d+)/(\d+)", "rewritten?var1=$1&var2=$2", 
                skipRemainingRules: true)
            .AddApacheModRewrite(apacheModRewriteStreamReader)
            .AddIISUrlRewrite(iisUrlRewriteStreamReader)
            .Add(MethodRules.RedirectXmlFileRequests)
            .Add(MethodRules.RewriteTextFileRequests)
            .Add(new RedirectImageRequests(".png", "/png-images"))
            .Add(new RedirectImageRequests(".jpg", "/jpg-images"));

        app.UseRewriter(options);
    }

    app.UseStaticFiles();

    app.Run(context => context.Response.WriteAsync(
        $"Rewritten or Redirected Url: " +
        $"{context.Request.Path + context.Request.QueryString}"));
}

原始请求:/rewrite-rule/1234/5678

Browser window with developer tools tracking the request and response: Add rewrite

表达式开头的脱字号 (^) 意味着匹配从 URL 路径的开头处开始。

在前面的重定向规则 redirect-rule/(.*) 的示例中,正则表达式的开头没有脱字号 (^)。 因此,路径中 redirect-rule/ 之前的任何字符都可能成功匹配。

路径 匹配
/redirect-rule/1234/5678
/my-cool-redirect-rule/1234/5678
/anotherredirect-rule/1234/5678

重写规则 ^rewrite-rule/(\d+)/(\d+) 只能与以 rewrite-rule/ 开头的路径匹配。 注意下表中的匹配差异。

路径 匹配
/rewrite-rule/1234/5678
/my-cool-rewrite-rule/1234/5678
/anotherrewrite-rule/1234/5678

在表达式的 ^rewrite-rule/ 部分之后,有两个捕获组 (\d+)/(\d+)\d 表示与数字匹配。 加号 (+) 表示与前面的一个或多个字符匹配。 因此,URL 必须包含数字加正斜杠加另一个数字的形式。 这些捕获组以 $1$2 的形式注入重写 URL 中。 重写规则替换字符串将捕获组放入查询字符串中。 重写 /rewrite-rule/1234/5678 的请求路径,获取 /rewritten?var1=1234&var2=5678 处的资源。 如果原始请求中存在查询字符串,则重写 URL 时会保留此字符串。

无需往返服务器来获取资源。 如果资源存在,系统会提取资源并以“200(正常)”状态代码返回给客户端。 因为客户端不会被重定向,所以浏览器地址栏中的 URL 不会发生更改。 客户端无法检测到服务器上发生的 URL 重写操作。

注意

尽可能使用 skipRemainingRules: true,因为匹配规则在计算上很昂贵并且增加了应用响应时间。 对于最快的应用响应:

  • 按照从最频繁匹配的规则到最不频繁匹配的规则排列重写规则。
  • 如果出现匹配项且无需处理任何其他规则,则跳过剩余规则的处理。

Apache mod_rewrite

使用 AddApacheModRewrite 应用 Apache mod_rewrite 规则。 请确保将规则文件与应用一起部署。 有关 mod_rewrite 规则的详细信息和示例,请参阅 Apache mod_rewrite

StreamReader 用于读取 ApacheModRewrite.txt 规则文件中的规则:

public void Configure(IApplicationBuilder app)
{
    using (StreamReader apacheModRewriteStreamReader = 
        File.OpenText("ApacheModRewrite.txt"))
    using (StreamReader iisUrlRewriteStreamReader = 
        File.OpenText("IISUrlRewrite.xml")) 
    {
        var options = new RewriteOptions()
            .AddRedirect("redirect-rule/(.*)", "redirected/$1")
            .AddRewrite(@"^rewrite-rule/(\d+)/(\d+)", "rewritten?var1=$1&var2=$2", 
                skipRemainingRules: true)
            .AddApacheModRewrite(apacheModRewriteStreamReader)
            .AddIISUrlRewrite(iisUrlRewriteStreamReader)
            .Add(MethodRules.RedirectXmlFileRequests)
            .Add(MethodRules.RewriteTextFileRequests)
            .Add(new RedirectImageRequests(".png", "/png-images"))
            .Add(new RedirectImageRequests(".jpg", "/jpg-images"));

        app.UseRewriter(options);
    }

    app.UseStaticFiles();

    app.Run(context => context.Response.WriteAsync(
        $"Rewritten or Redirected Url: " +
        $"{context.Request.Path + context.Request.QueryString}"));
}

示例应用将请求从 /apache-mod-rules-redirect/(.\*) 重定向到 /redirected?id=$1。 响应状态代码为“302 (已找到)”。

# Rewrite path with additional sub directory
RewriteRule ^/apache-mod-rules-redirect/(.*) /redirected?id=$1 [L,R=302]

原始请求:/apache-mod-rules-redirect/1234

Browser window with developer tools tracking the requests and responses: Add Apache mod redirect

中间件支持下列 Apache mod_rewrite 服务器变量:

  • CONN_REMOTE_ADDR
  • HTTP_ACCEPT
  • HTTP_CONNECTION
  • HTTP_COOKIE
  • HTTP_FORWARDED
  • HTTP_HOST
  • HTTP_REFERER
  • HTTP_USER_AGENT
  • HTTPS
  • IPV6
  • QUERY_STRING
  • REMOTE_ADDR
  • REMOTE_PORT
  • REQUEST_FILENAME
  • REQUEST_METHOD
  • REQUEST_SCHEME
  • REQUEST_URI
  • SCRIPT_FILENAME
  • SERVER_ADDR
  • SERVER_PORT
  • SERVER_PROTOCOL
  • TIME
  • TIME_DAY
  • TIME_HOUR
  • TIME_MIN
  • TIME_MON
  • TIME_SEC
  • TIME_WDAY
  • TIME_YEAR

IIS URL 重写模块规则

若要使用适用于 IIS URL 重写模块的同一规则集,使用 AddIISUrlRewrite。 请确保将规则文件与应用一起部署。 当在 Windows Server IIS 上运行时,请勿指示中间件使用应用的 web.config 文件。 使用 IIS 时,应将这些规则存储在应用的 web.config 文件之外,以避免与 IIS 重写模块发生冲突。 有关 IIS URL 重写模块规则的详细信息和示例,请参阅 Using Url Rewrite Module 2.0(使用 URL 重写模块 2.0)和 URL Rewrite Module Configuration Reference(URL 重写模块配置引用)。

StreamReader 用于读取 IISUrlRewrite.xml 规则文件中的规则:

public void Configure(IApplicationBuilder app)
{
    using (StreamReader apacheModRewriteStreamReader = 
        File.OpenText("ApacheModRewrite.txt"))
    using (StreamReader iisUrlRewriteStreamReader = 
        File.OpenText("IISUrlRewrite.xml")) 
    {
        var options = new RewriteOptions()
            .AddRedirect("redirect-rule/(.*)", "redirected/$1")
            .AddRewrite(@"^rewrite-rule/(\d+)/(\d+)", "rewritten?var1=$1&var2=$2", 
                skipRemainingRules: true)
            .AddApacheModRewrite(apacheModRewriteStreamReader)
            .AddIISUrlRewrite(iisUrlRewriteStreamReader)
            .Add(MethodRules.RedirectXmlFileRequests)
            .Add(MethodRules.RewriteTextFileRequests)
            .Add(new RedirectImageRequests(".png", "/png-images"))
            .Add(new RedirectImageRequests(".jpg", "/jpg-images"));

        app.UseRewriter(options);
    }

    app.UseStaticFiles();

    app.Run(context => context.Response.WriteAsync(
        $"Rewritten or Redirected Url: " +
        $"{context.Request.Path + context.Request.QueryString}"));
}

示例应用将请求从 /iis-rules-rewrite/(.*) 重写为 /rewritten?id=$1。 以“200 (正常)”状态代码作为响应发送到客户端。

<rewrite>
  <rules>
    <rule name="Rewrite segment to id querystring" stopProcessing="true">
      <match url="^iis-rules-rewrite/(.*)$" />
      <action type="Rewrite" url="rewritten?id={R:1}" appendQueryString="false"/>
    </rule>
  </rules>
</rewrite>

原始请求:/iis-rules-rewrite/1234

Browser window with developer tools tracking the request and response: Add IIS URL rewrite

如果有配置了服务器级别规则(可对应用产生不利影响)的活动 IIS 重写模块,则可禁用应用的 IIS 重写模块。 有关详细信息,请参阅禁用 IIS 模块

不支持的功能

中间件不支持以下 IIS URL 重写模块功能:

  • 出站规则
  • 自定义服务器变量
  • 通配符
  • LogRewrittenUrl

受支持的服务器变量

中间件支持下列 IIS URL 重写模块服务器变量:

  • CONTENT_LENGTH
  • CONTENT_TYPE
  • HTTP_ACCEPT
  • HTTP_CONNECTION
  • HTTP_COOKIE
  • HTTP_HOST
  • HTTP_REFERER
  • HTTP_URL
  • HTTP_USER_AGENT
  • HTTPS
  • LOCAL_ADDR
  • QUERY_STRING
  • REMOTE_ADDR
  • REMOTE_PORT
  • REQUEST_FILENAME
  • REQUEST_URI

注意

也可通过 PhysicalFileProvider 获取 IFileProvider。 这种方法可为重写规则文件的位置提供更大的灵活性。 请确保将重写规则文件部署到所提供路径的服务器中。

PhysicalFileProvider fileProvider = new PhysicalFileProvider(Directory.GetCurrentDirectory());

基于方法的规则

使用 Add 在方法中实现自己的规则逻辑。 Add 公开 RewriteContext,这使 HttpContext 可用于方法中。 RewriteContext.Result 决定如何处理其他管道进程。 将值设置为下表中的 RuleResult 字段之一。

重写上下文结果 操作
RuleResult.ContinueRules(默认值) 继续应用规则。
RuleResult.EndResponse 停止应用规则并发送响应。
RuleResult.SkipRemainingRules 停止应用规则并将上下文发送给下一个中间件。
public void Configure(IApplicationBuilder app)
{
    using (StreamReader apacheModRewriteStreamReader = 
        File.OpenText("ApacheModRewrite.txt"))
    using (StreamReader iisUrlRewriteStreamReader = 
        File.OpenText("IISUrlRewrite.xml")) 
    {
        var options = new RewriteOptions()
            .AddRedirect("redirect-rule/(.*)", "redirected/$1")
            .AddRewrite(@"^rewrite-rule/(\d+)/(\d+)", "rewritten?var1=$1&var2=$2", 
                skipRemainingRules: true)
            .AddApacheModRewrite(apacheModRewriteStreamReader)
            .AddIISUrlRewrite(iisUrlRewriteStreamReader)
            .Add(MethodRules.RedirectXmlFileRequests)
            .Add(MethodRules.RewriteTextFileRequests)
            .Add(new RedirectImageRequests(".png", "/png-images"))
            .Add(new RedirectImageRequests(".jpg", "/jpg-images"));

        app.UseRewriter(options);
    }

    app.UseStaticFiles();

    app.Run(context => context.Response.WriteAsync(
        $"Rewritten or Redirected Url: " +
        $"{context.Request.Path + context.Request.QueryString}"));
}

示例应用演示了如何对以 .xml 结尾的路径的请求进行重定向。 如果发出针对 /file.xml 的请求,请求将重定向到 /xmlfiles/file.xml。 状态代码设置为 301 - Moved Permanently。 当浏览器发出针对 /xmlfiles/file.xml 的新请求后,静态文件中间件会将文件从 wwwroot/xmlfiles 文件夹提供给客户端。 对于重定向,请显式设置响应的状态代码。 否则,将会返回“200 (正常)”状态代码,且客户端上不会发生重写。

RewriteRules.cs

public static void RedirectXmlFileRequests(RewriteContext context)
{
    var request = context.HttpContext.Request;

    // Because the client is redirecting back to the same app, stop 
    // processing if the request has already been redirected.
    if (request.Path.StartsWithSegments(new PathString("/xmlfiles")))
    {
        return;
    }

    if (request.Path.Value.EndsWith(".xml", StringComparison.OrdinalIgnoreCase))
    {
        var response = context.HttpContext.Response;
        response.StatusCode = (int) HttpStatusCode.MovedPermanently;
        context.Result = RuleResult.EndResponse;
        response.Headers[HeaderNames.Location] = 
            "/xmlfiles" + request.Path + request.QueryString;
    }
}

此方法还可以重写请求。 示例应用演示了如何重写任何文本文件请求的路径以从 wwwroot 文件夹中提供 file.txt 文本文件 。 静态文件中间件基于更新的请求路径来提供文件:

public void Configure(IApplicationBuilder app)
{
    using (StreamReader apacheModRewriteStreamReader = 
        File.OpenText("ApacheModRewrite.txt"))
    using (StreamReader iisUrlRewriteStreamReader = 
        File.OpenText("IISUrlRewrite.xml")) 
    {
        var options = new RewriteOptions()
            .AddRedirect("redirect-rule/(.*)", "redirected/$1")
            .AddRewrite(@"^rewrite-rule/(\d+)/(\d+)", "rewritten?var1=$1&var2=$2", 
                skipRemainingRules: true)
            .AddApacheModRewrite(apacheModRewriteStreamReader)
            .AddIISUrlRewrite(iisUrlRewriteStreamReader)
            .Add(MethodRules.RedirectXmlFileRequests)
            .Add(MethodRules.RewriteTextFileRequests)
            .Add(new RedirectImageRequests(".png", "/png-images"))
            .Add(new RedirectImageRequests(".jpg", "/jpg-images"));

        app.UseRewriter(options);
    }

    app.UseStaticFiles();

    app.Run(context => context.Response.WriteAsync(
        $"Rewritten or Redirected Url: " +
        $"{context.Request.Path + context.Request.QueryString}"));
}

RewriteRules.cs

public static void RewriteTextFileRequests(RewriteContext context)
{
    var request = context.HttpContext.Request;

    if (request.Path.Value.EndsWith(".txt", StringComparison.OrdinalIgnoreCase))
    {
        context.Result = RuleResult.SkipRemainingRules;
        request.Path = "/file.txt";
    }
}

基于 IRule 的规则

使用 Add 在实现 IRule 接口的类中使用规则逻辑。 与使用基于方法的规则方法相比,IRule 提供了更大的灵活性。 实现类可能包含构造函数,可在其中传入 ApplyRule 方法的参数。

public void Configure(IApplicationBuilder app)
{
    using (StreamReader apacheModRewriteStreamReader = 
        File.OpenText("ApacheModRewrite.txt"))
    using (StreamReader iisUrlRewriteStreamReader = 
        File.OpenText("IISUrlRewrite.xml")) 
    {
        var options = new RewriteOptions()
            .AddRedirect("redirect-rule/(.*)", "redirected/$1")
            .AddRewrite(@"^rewrite-rule/(\d+)/(\d+)", "rewritten?var1=$1&var2=$2", 
                skipRemainingRules: true)
            .AddApacheModRewrite(apacheModRewriteStreamReader)
            .AddIISUrlRewrite(iisUrlRewriteStreamReader)
            .Add(MethodRules.RedirectXmlFileRequests)
            .Add(MethodRules.RewriteTextFileRequests)
            .Add(new RedirectImageRequests(".png", "/png-images"))
            .Add(new RedirectImageRequests(".jpg", "/jpg-images"));

        app.UseRewriter(options);
    }

    app.UseStaticFiles();

    app.Run(context => context.Response.WriteAsync(
        $"Rewritten or Redirected Url: " +
        $"{context.Request.Path + context.Request.QueryString}"));
}

检查示例应用中 extensionnewPath 的参数值是否符合多个条件。 extension 必须包含一个值,并且该值必须是 .png.jpg 或 .gif。 如果 newPath 无效,则会引发 ArgumentException。 如果发出针对 image.png 的请求,请求将重定向到 /png-images/image.png。 如果发出针对 image.jpg 的请求,请求将重定向到 /jpg-images/image.jpg。 状态代码设置为 301 - Moved Permanentlycontext.Result 设置为停止处理规则并发送响应。

public class RedirectImageRequests : IRule
{
    private readonly string _extension;
    private readonly PathString _newPath;

    public RedirectImageRequests(string extension, string newPath)
    {
        if (string.IsNullOrEmpty(extension))
        {
            throw new ArgumentException(nameof(extension));
        }

        if (!Regex.IsMatch(extension, @"^\.(png|jpg|gif)$"))
        {
            throw new ArgumentException("Invalid extension", nameof(extension));
        }

        if (!Regex.IsMatch(newPath, @"(/[A-Za-z0-9]+)+?"))
        {
            throw new ArgumentException("Invalid path", nameof(newPath));
        }

        _extension = extension;
        _newPath = new PathString(newPath);
    }

    public void ApplyRule(RewriteContext context)
    {
        var request = context.HttpContext.Request;

        // Because we're redirecting back to the same app, stop 
        // processing if the request has already been redirected
        if (request.Path.StartsWithSegments(new PathString(_newPath)))
        {
            return;
        }

        if (request.Path.Value.EndsWith(_extension, StringComparison.OrdinalIgnoreCase))
        {
            var response = context.HttpContext.Response;
            response.StatusCode = (int) HttpStatusCode.MovedPermanently;
            context.Result = RuleResult.EndResponse;
            response.Headers[HeaderNames.Location] = 
                _newPath + request.Path + request.QueryString;
        }
    }
}

原始请求:/image.png

Browser window with developer tools tracking the requests and responses for image.png

原始请求:/image.jpg

Browser window with developer tools tracking the requests and responses for image.jpg

正则表达式示例

目标 正则表达式字符串和
匹配示例
替换字符串和
输出示例
将路径重写为查询字符串 ^path/(.*)/(.*)
/path/abc/123
path?var1=$1&var2=$2
/path?var1=abc&var2=123
去掉尾部反斜杠 (.*)/$
/path/
$1
/path
强制添加尾部反斜杠 (.*[^/])$
/path
$1/
/path/
避免重写特定请求 ^(.*)(?<!\.axd)$^(?!.*\.axd$)(.*)$
正确:/resource.htm
错误:/resource.axd
rewritten/$1
/rewritten/resource.htm
/resource.axd
重新排列 URL 段 path/(.*)/(.*)/(.*)
path/1/2/3
path/$3/$2/$1
path/3/2/1
替换 URL 段 ^(.*)/segment2/(.*)
/segment1/segment2/segment3
$1/replaced/$2
/segment1/replaced/segment3

本文档介绍 URL 重写并说明如何在 ASP.NET Core 应用中使用 URL 重写中间件。

URL 重写是根据一个或多个预定义规则修改请求 URL 的行为。 URL 重写会在资源位置和地址之间创建一个抽象,使位置和地址不紧密相连。 在以下几种方案中,URL 重写很有价值:

  • 暂时或永久移动或替换服务器资源,并维护这些资源的稳定定位符。
  • 拆分在不同应用或同一应用的不同区域中处理的请求。
  • 删除、添加或重新组织传入请求上的 URL 段。
  • 优化搜索引擎优化 (SEO) 的公共 URL。
  • 允许使用友好的公共 URL 来帮助访问者预测请求资源后返回的内容。
  • 将不安全请求重定向到安全终结点。
  • 防止热链接,外部站点会通过热链接将其他站点的资产链接到其自己的内容,从而利用托管在其他站点上的静态资产。

注意

URL 重写可能会降低应用的性能。 如果可行,应限制规则的数量和复杂度。

查看或下载示例代码如何下载

URL 重定向和 URL 重写

URL 重定向和 URL 重写之间的用词差异很细微,但这对于向客户端提供资源具有重要意义 。 ASP.NET Core 的 URL 重写中间件能够满足两者的需求。

URL 重定向涉及客户端操作,指示客户端访问与客户端最初请求地址不同的资源。 这需要往返服务器。 客户端对资源发出新请求时,返回客户端的重定向 URL 会出现在浏览器地址栏。

如果 /resource 被重定向到 /different-resource,则服务器作出响应,指示客户端应在 /different-resource 获取资源,所提供的状态代码指示重定向是临时的还是永久的。

A WebAPI service endpoint has been temporarily changed from version 1 (v1) to version 2 (v2) on the server. A client makes a request to the service at the version 1 path /v1/api. The server sends back a 302 (Found) response with the new, temporary path for the service at version 2 /v2/api. The client makes a second request to the service at the redirect URL. The server responds with a 200 (OK) status code.

将请求重定向到不同 URL 时,通过使用响应指定状态代码来指示重定向是永久还是临时:

  • 如果资源有一个新的永久性 URL,并且你希望指示客户端所有将来的资源请求都使用新 URL,则使用 301 - Moved Permanently 状态代码。 收到 301 状态代码时,客户端可能会缓存响应并重用这段代码。

  • “302 (找到)”状态代码用于后列情况:重定向操作是临时的或通常会发生变化。 302 状态代码向客户端指示不存储 URL 并在将来使用。

有关状态代码的详细信息,请参阅 RFC 9110:状态代码定义

URL 重写是服务器端操作,它从与客户端请求的资源地址不同的资源地址提供资源。 重写 URL 不需要往返服务器。 重写的 URL 不会返回客户端,也不会出现在浏览器地址栏。

如果 /resource 重写到 /different-resource,服务器会在内部提取并返回 /different-resource 处的资源 。

尽管客户端可能能够检索已重写 URL 处的资源,但是,客户端发出请求并收到响应时,并不知道已重写 URL 处存在的资源。

A WebAPI service endpoint has been changed from version 1 (v1) to version 2 (v2) on the server. A client makes a request to the service at the version 1 path /v1/api. The request URL is rewritten to access the service at the version 2 path /v2/api. The service responds to the client with a 200 (OK) status code.

URL 重写示例应用

可使用示例应用了解 URL 重写中间件的功能。 该应用程序应用重定向和重写规则,并显示多个方案的重定向或重写的 URL。

何时使用 URL 重写中间件

如果无法使用以下方法,请使用 URL 重写中间件:

此外,如果应用程序在 HTTP.sys 服务器(旧称 WebListener)上托管,请使用中间件。

使用 IIS、Apache 和 Nginx 中的基于服务器的 URL 重写技术的主要原因:

  • 中间件不支持这些模块的完整功能。

    服务器模块的一些功能不适用于 ASP.NET Core 项目,例如 IIS 重写模块的 IsFileIsDirectory 约束。 在这些情况下,请改为使用中间件。

  • 中间件性能与模块性能不匹配。

    基准测试是确定哪种方法会最大程度降低性能或降低的性能是否可忽略不计的唯一方法。

Package

要在项目中包含中间件,请在项目文件中添加对 Microsoft.AspNetCore.App 元数据包的包引用,该文件包含 Microsoft.AspNetCore.Rewrite 包。

不使用 Microsoft.AspNetCore.App 元包时,向 Microsoft.AspNetCore.Rewrite 包添加项目引用。

扩展和选项

通过使用扩展方法为每条重写规则创建 RewriteOptions 类的实例,建立 URL 重写和重写定向规则。 按所需的处理顺序链接多个规则。 使用 UseRewriterRewriteOptions 添加到请求管道时,它会被传递到 URL 重写中间件:

public void Configure(IApplicationBuilder app)
{
    using (StreamReader apacheModRewriteStreamReader = 
        File.OpenText("ApacheModRewrite.txt"))
    using (StreamReader iisUrlRewriteStreamReader = 
        File.OpenText("IISUrlRewrite.xml")) 
    {
        var options = new RewriteOptions()
            .AddRedirect("redirect-rule/(.*)", "redirected/$1")
            .AddRewrite(@"^rewrite-rule/(\d+)/(\d+)", "rewritten?var1=$1&var2=$2", 
                skipRemainingRules: true)
            .AddApacheModRewrite(apacheModRewriteStreamReader)
            .AddIISUrlRewrite(iisUrlRewriteStreamReader)
            .Add(MethodRules.RedirectXmlFileRequests)
            .Add(MethodRules.RewriteTextFileRequests)
            .Add(new RedirectImageRequests(".png", "/png-images"))
            .Add(new RedirectImageRequests(".jpg", "/jpg-images"));

        app.UseRewriter(options);
    }

    app.UseStaticFiles();

    app.Run(context => context.Response.WriteAsync(
        $"Rewritten or Redirected Url: " +
        $"{context.Request.Path + context.Request.QueryString}"));
}

将非 www 重定向到 www

三个选项允许应用将非 www 重新定向到 www

URL 重定向

使用 AddRedirect 将请求重定向。 第一个参数包含用于匹配传入 URL 路径的正则表达式。 第二个参数是替换字符串。 第三个参数(如有)指定状态代码。 如不指定状态代码,则状态代码默认为“302 (已找到)”,指示资源暂时移动或替换。

public void Configure(IApplicationBuilder app)
{
    using (StreamReader apacheModRewriteStreamReader = 
        File.OpenText("ApacheModRewrite.txt"))
    using (StreamReader iisUrlRewriteStreamReader = 
        File.OpenText("IISUrlRewrite.xml")) 
    {
        var options = new RewriteOptions()
            .AddRedirect("redirect-rule/(.*)", "redirected/$1")
            .AddRewrite(@"^rewrite-rule/(\d+)/(\d+)", "rewritten?var1=$1&var2=$2", 
                skipRemainingRules: true)
            .AddApacheModRewrite(apacheModRewriteStreamReader)
            .AddIISUrlRewrite(iisUrlRewriteStreamReader)
            .Add(MethodRules.RedirectXmlFileRequests)
            .Add(MethodRules.RewriteTextFileRequests)
            .Add(new RedirectImageRequests(".png", "/png-images"))
            .Add(new RedirectImageRequests(".jpg", "/jpg-images"));

        app.UseRewriter(options);
    }

    app.UseStaticFiles();

    app.Run(context => context.Response.WriteAsync(
        $"Rewritten or Redirected Url: " +
        $"{context.Request.Path + context.Request.QueryString}"));
}

在启用了开发人员工具的浏览器中,向路径为 /redirect-rule/1234/5678 的示例应用发出请求。 正则表达式匹配 redirect-rule/(.*) 上的请求路径,且该路径会被 /redirected/1234/5678 替代。 重定向 URL 以“302 (已找到)”状态代码发回客户端。 浏览器会在浏览器地址栏中出现的重定向 URL 上发出新请求。 由于示例应用中的规则都不匹配重定向 URL:

  • 第二个请求从应用程序收到“200 (正常)”响应。
  • 响应正文显示了重定向 URL。

重定向 URL 时,系统将向服务器进行一次往返。

警告

建立重定向规则时务必小心。 系统会根据应用的每个请求(包括重定向后的请求)对重定向规则进行评估。 很容易便会意外创建无限重定向循环。

原始请求:/redirect-rule/1234/5678

Add redirect: Browser window with developer tools tracking the requests and responses

括号内的表达式部分称为“捕获组”。 表达式的点 (.) 表示匹配任何字符。 星号 (*) 表示零次或多次匹配前面的字符。 因此,URL 的最后两个路径段 1234/5678 由捕获组 (.*) 捕获。 在请求 URL 中提供的位于 redirect-rule/ 之后的任何值均由此单个捕获组捕获。

在替换字符串中,将捕获组注入带有美元符号 ($)、后跟捕获序列号的字符串中。 获取的第一个捕获组值为 $1,第二个为 $2,并且正则表达式中的其他捕获组值将依次继续排列。 示例应用的重定向规则正则表达式中只有一个捕获组,因此替换字符串中只有一个注入组,即 $1。 如果应用此规则,URL 将变为 /redirected/1234/5678

URL 重定向到安全的终结点

使用 AddRedirectToHttps 将 HTTP 请求重定向到采用 HTTPS 协议的相同主机和路径。 如不提供状态代码,则中间件默认为“302(已找到)”。 如果不提供端口:

  • 中间件默认为 null
  • 方案更改为 https(HTTPS 协议),客户端访问端口 443 上的资源。

下面的示例展示了如何将状态代码设置为 301 - Moved Permanently 并将端口更改为 5001。

public void Configure(IApplicationBuilder app)
{
    var options = new RewriteOptions()
        .AddRedirectToHttps(301, 5001);

    app.UseRewriter(options);
}

使用 AddRedirectToHttpsPermanent 将不安全的请求重定向到端口 443 上的采用安全 HTTPS 协议的相同主机和路径。 中间件将状态代码设置为 301 - Moved Permanently

public void Configure(IApplicationBuilder app)
{
    var options = new RewriteOptions()
        .AddRedirectToHttpsPermanent();

    app.UseRewriter(options);
}

注意

当重定向到安全的终结点并且不需要其他重定向规则时,建议使用 HTTPS 重定向中间件。 有关详细信息,请参阅强制使用 HTTPS主题。

示例应用能够演示如何使用 AddRedirectToHttpsAddRedirectToHttpsPermanent。 将扩展方法添加到 RewriteOptions。 在任何 URL 上向应用发出不安全的请求。 消除自签名证书不受信任的浏览器安全警告,或创建例外以信任证书。

使用 AddRedirectToHttps(301, 5001) 的原始请求:http://localhost:5000/secure

Add redirect to HTTPS: Browser window with developer tools tracking the requests and responses

使用 AddRedirectToHttpsPermanent 的原始请求:http://localhost:5000/secure

Add redirect to HTTPS permanent: Browser window with developer tools tracking the requests and responses

URL 重写

使用 AddRewrite 创建重写 URL 的规则。 第一个参数包含用于匹配传入 URL 路径的正则表达式。 第二个参数是替换字符串。 第三个参数 skipRemainingRules: {true|false} 指示如果当前规则适用,中间件是否要跳过其他重写规则。

public void Configure(IApplicationBuilder app)
{
    using (StreamReader apacheModRewriteStreamReader = 
        File.OpenText("ApacheModRewrite.txt"))
    using (StreamReader iisUrlRewriteStreamReader = 
        File.OpenText("IISUrlRewrite.xml")) 
    {
        var options = new RewriteOptions()
            .AddRedirect("redirect-rule/(.*)", "redirected/$1")
            .AddRewrite(@"^rewrite-rule/(\d+)/(\d+)", "rewritten?var1=$1&var2=$2", 
                skipRemainingRules: true)
            .AddApacheModRewrite(apacheModRewriteStreamReader)
            .AddIISUrlRewrite(iisUrlRewriteStreamReader)
            .Add(MethodRules.RedirectXmlFileRequests)
            .Add(MethodRules.RewriteTextFileRequests)
            .Add(new RedirectImageRequests(".png", "/png-images"))
            .Add(new RedirectImageRequests(".jpg", "/jpg-images"));

        app.UseRewriter(options);
    }

    app.UseStaticFiles();

    app.Run(context => context.Response.WriteAsync(
        $"Rewritten or Redirected Url: " +
        $"{context.Request.Path + context.Request.QueryString}"));
}

原始请求:/rewrite-rule/1234/5678

Add rewrite: Browser window with developer tools tracking the request and response

表达式开头的脱字号 (^) 意味着匹配从 URL 路径的开头处开始。

在前面的重定向规则 redirect-rule/(.*) 的示例中,正则表达式的开头没有脱字号 (^)。 因此,路径中 redirect-rule/ 之前的任何字符都可能成功匹配。

路径 匹配
/redirect-rule/1234/5678
/my-cool-redirect-rule/1234/5678
/anotherredirect-rule/1234/5678

重写规则 ^rewrite-rule/(\d+)/(\d+) 只能与以 rewrite-rule/ 开头的路径匹配。 注意下表中的匹配差异。

路径 匹配
/rewrite-rule/1234/5678
/my-cool-rewrite-rule/1234/5678
/anotherrewrite-rule/1234/5678

在表达式的 ^rewrite-rule/ 部分之后,有两个捕获组 (\d+)/(\d+)\d 表示与数字匹配。 加号 (+) 表示与前面的一个或多个字符匹配。 因此,URL 必须包含数字加正斜杠加另一个数字的形式。 这些捕获组以 $1$2 的形式注入重写 URL 中。 重写规则替换字符串将捕获组放入查询字符串中。 重写 /rewrite-rule/1234/5678 的请求路径,获取 /rewritten?var1=1234&var2=5678 处的资源。 如果原始请求中存在查询字符串,则重写 URL 时会保留此字符串。

无需往返服务器来获取资源。 如果资源存在,系统会提取资源并以“200(正常)”状态代码返回给客户端。 因为客户端不会被重定向,所以浏览器地址栏中的 URL 不会发生更改。 客户端无法检测到服务器上发生的 URL 重写操作。

注意

尽可能使用 skipRemainingRules: true,因为匹配规则在计算上很昂贵并且增加了应用响应时间。 对于最快的应用响应:

  • 按照从最频繁匹配的规则到最不频繁匹配的规则排列重写规则。
  • 如果出现匹配项且无需处理任何其他规则,则跳过剩余规则的处理。

Apache mod_rewrite

使用 AddApacheModRewrite 应用 Apache mod_rewrite 规则。 请确保将规则文件与应用一起部署。 有关 mod_rewrite 规则的详细信息和示例,请参阅 Apache mod_rewrite

StreamReader 用于读取 ApacheModRewrite.txt 规则文件中的规则:

public void Configure(IApplicationBuilder app)
{
    using (StreamReader apacheModRewriteStreamReader = 
        File.OpenText("ApacheModRewrite.txt"))
    using (StreamReader iisUrlRewriteStreamReader = 
        File.OpenText("IISUrlRewrite.xml")) 
    {
        var options = new RewriteOptions()
            .AddRedirect("redirect-rule/(.*)", "redirected/$1")
            .AddRewrite(@"^rewrite-rule/(\d+)/(\d+)", "rewritten?var1=$1&var2=$2", 
                skipRemainingRules: true)
            .AddApacheModRewrite(apacheModRewriteStreamReader)
            .AddIISUrlRewrite(iisUrlRewriteStreamReader)
            .Add(MethodRules.RedirectXmlFileRequests)
            .Add(MethodRules.RewriteTextFileRequests)
            .Add(new RedirectImageRequests(".png", "/png-images"))
            .Add(new RedirectImageRequests(".jpg", "/jpg-images"));

        app.UseRewriter(options);
    }

    app.UseStaticFiles();

    app.Run(context => context.Response.WriteAsync(
        $"Rewritten or Redirected Url: " +
        $"{context.Request.Path + context.Request.QueryString}"));
}

示例应用将请求从 /apache-mod-rules-redirect/(.\*) 重定向到 /redirected?id=$1。 响应状态代码为“302 (已找到)”。

# Rewrite path with additional sub directory
RewriteRule ^/apache-mod-rules-redirect/(.*) /redirected?id=$1 [L,R=302]

原始请求:/apache-mod-rules-redirect/1234

Add Apache mod redirect: Browser window with developer tools tracking the requests and responses

中间件支持下列 Apache mod_rewrite 服务器变量:

  • CONN_REMOTE_ADDR
  • HTTP_ACCEPT
  • HTTP_CONNECTION
  • HTTP_COOKIE
  • HTTP_FORWARDED
  • HTTP_HOST
  • HTTP_REFERER
  • HTTP_USER_AGENT
  • HTTPS
  • IPV6
  • QUERY_STRING
  • REMOTE_ADDR
  • REMOTE_PORT
  • REQUEST_FILENAME
  • REQUEST_METHOD
  • REQUEST_SCHEME
  • REQUEST_URI
  • SCRIPT_FILENAME
  • SERVER_ADDR
  • SERVER_PORT
  • SERVER_PROTOCOL
  • TIME
  • TIME_DAY
  • TIME_HOUR
  • TIME_MIN
  • TIME_MON
  • TIME_SEC
  • TIME_WDAY
  • TIME_YEAR

IIS URL 重写模块规则

若要使用适用于 IIS URL 重写模块的同一规则集,使用 AddIISUrlRewrite。 请确保将规则文件与应用一起部署。 当在 Windows Server IIS 上运行时,请勿指示中间件使用应用的 web.config 文件。 使用 IIS 时,应将这些规则存储在应用的 web.config 文件之外,以避免与 IIS 重写模块发生冲突。 有关 IIS URL 重写模块规则的详细信息和示例,请参阅 Using Url Rewrite Module 2.0(使用 URL 重写模块 2.0)和 URL Rewrite Module Configuration Reference(URL 重写模块配置引用)。

StreamReader 用于读取 IISUrlRewrite.xml 规则文件中的规则:

public void Configure(IApplicationBuilder app)
{
    using (StreamReader apacheModRewriteStreamReader = 
        File.OpenText("ApacheModRewrite.txt"))
    using (StreamReader iisUrlRewriteStreamReader = 
        File.OpenText("IISUrlRewrite.xml")) 
    {
        var options = new RewriteOptions()
            .AddRedirect("redirect-rule/(.*)", "redirected/$1")
            .AddRewrite(@"^rewrite-rule/(\d+)/(\d+)", "rewritten?var1=$1&var2=$2", 
                skipRemainingRules: true)
            .AddApacheModRewrite(apacheModRewriteStreamReader)
            .AddIISUrlRewrite(iisUrlRewriteStreamReader)
            .Add(MethodRules.RedirectXmlFileRequests)
            .Add(MethodRules.RewriteTextFileRequests)
            .Add(new RedirectImageRequests(".png", "/png-images"))
            .Add(new RedirectImageRequests(".jpg", "/jpg-images"));

        app.UseRewriter(options);
    }

    app.UseStaticFiles();

    app.Run(context => context.Response.WriteAsync(
        $"Rewritten or Redirected Url: " +
        $"{context.Request.Path + context.Request.QueryString}"));
}

示例应用将请求从 /iis-rules-rewrite/(.*) 重写为 /rewritten?id=$1。 以“200 (正常)”状态代码作为响应发送到客户端。

<rewrite>
  <rules>
    <rule name="Rewrite segment to id querystring" stopProcessing="true">
      <match url="^iis-rules-rewrite/(.*)$" />
      <action type="Rewrite" url="rewritten?id={R:1}" appendQueryString="false"/>
    </rule>
  </rules>
</rewrite>

原始请求:/iis-rules-rewrite/1234

Add IIS URL rewrite: Browser window with developer tools tracking the request and response

如果有配置了服务器级别规则(可对应用产生不利影响)的活动 IIS 重写模块,则可禁用应用的 IIS 重写模块。 有关详细信息,请参阅禁用 IIS 模块

不支持的功能

与 ASP.NET Core 2.x 一同发布的中间件不支持以下 IIS URL 重写模块功能:

  • 出站规则
  • 自定义服务器变量
  • 通配符
  • LogRewrittenUrl

受支持的服务器变量

中间件支持下列 IIS URL 重写模块服务器变量:

  • CONTENT_LENGTH
  • CONTENT_TYPE
  • HTTP_ACCEPT
  • HTTP_CONNECTION
  • HTTP_COOKIE
  • HTTP_HOST
  • HTTP_REFERER
  • HTTP_URL
  • HTTP_USER_AGENT
  • HTTPS
  • LOCAL_ADDR
  • QUERY_STRING
  • REMOTE_ADDR
  • REMOTE_PORT
  • REQUEST_FILENAME
  • REQUEST_URI

注意

也可通过 PhysicalFileProvider 获取 IFileProvider。 这种方法可为重写规则文件的位置提供更大的灵活性。 请确保将重写规则文件部署到所提供路径的服务器中。

PhysicalFileProvider fileProvider = new PhysicalFileProvider(Directory.GetCurrentDirectory());

基于方法的规则

使用 Add 在方法中实现自己的规则逻辑。 Add 公开 RewriteContext,这使 HttpContext 可用于方法中。 RewriteContext.Result 决定如何处理其他管道进程。 将值设置为下表中的 RuleResult 字段之一。

重写上下文结果 操作
RuleResult.ContinueRules(默认值) 继续应用规则。
RuleResult.EndResponse 停止应用规则并发送响应。
RuleResult.SkipRemainingRules 停止应用规则并将上下文发送给下一个中间件。
public void Configure(IApplicationBuilder app)
{
    using (StreamReader apacheModRewriteStreamReader = 
        File.OpenText("ApacheModRewrite.txt"))
    using (StreamReader iisUrlRewriteStreamReader = 
        File.OpenText("IISUrlRewrite.xml")) 
    {
        var options = new RewriteOptions()
            .AddRedirect("redirect-rule/(.*)", "redirected/$1")
            .AddRewrite(@"^rewrite-rule/(\d+)/(\d+)", "rewritten?var1=$1&var2=$2", 
                skipRemainingRules: true)
            .AddApacheModRewrite(apacheModRewriteStreamReader)
            .AddIISUrlRewrite(iisUrlRewriteStreamReader)
            .Add(MethodRules.RedirectXmlFileRequests)
            .Add(MethodRules.RewriteTextFileRequests)
            .Add(new RedirectImageRequests(".png", "/png-images"))
            .Add(new RedirectImageRequests(".jpg", "/jpg-images"));

        app.UseRewriter(options);
    }

    app.UseStaticFiles();

    app.Run(context => context.Response.WriteAsync(
        $"Rewritten or Redirected Url: " +
        $"{context.Request.Path + context.Request.QueryString}"));
}

示例应用演示了如何对以 .xml 结尾的路径的请求进行重定向。 如果发出针对 /file.xml 的请求,请求将重定向到 /xmlfiles/file.xml。 状态代码设置为 301 - Moved Permanently。 当浏览器发出针对 /xmlfiles/file.xml 的新请求后,静态文件中间件会将文件从 wwwroot/xmlfiles 文件夹提供给客户端。 对于重定向,请显式设置响应的状态代码。 否则,将会返回“200 (正常)”状态代码,且客户端上不会发生重写。

RewriteRules.cs

public static void RedirectXmlFileRequests(RewriteContext context)
{
    var request = context.HttpContext.Request;

    // Because the client is redirecting back to the same app, stop 
    // processing if the request has already been redirected.
    if (request.Path.StartsWithSegments(new PathString("/xmlfiles")))
    {
        return;
    }

    if (request.Path.Value.EndsWith(".xml", StringComparison.OrdinalIgnoreCase))
    {
        var response = context.HttpContext.Response;
        response.StatusCode = (int) HttpStatusCode.MovedPermanently;
        context.Result = RuleResult.EndResponse;
        response.Headers[HeaderNames.Location] = 
            "/xmlfiles" + request.Path + request.QueryString;
    }
}

此方法还可以重写请求。 示例应用演示了如何重写任何文本文件请求的路径以从 wwwroot 文件夹中提供 file.txt 文本文件 。 静态文件中间件基于更新的请求路径来提供文件:

public void Configure(IApplicationBuilder app)
{
    using (StreamReader apacheModRewriteStreamReader = 
        File.OpenText("ApacheModRewrite.txt"))
    using (StreamReader iisUrlRewriteStreamReader = 
        File.OpenText("IISUrlRewrite.xml")) 
    {
        var options = new RewriteOptions()
            .AddRedirect("redirect-rule/(.*)", "redirected/$1")
            .AddRewrite(@"^rewrite-rule/(\d+)/(\d+)", "rewritten?var1=$1&var2=$2", 
                skipRemainingRules: true)
            .AddApacheModRewrite(apacheModRewriteStreamReader)
            .AddIISUrlRewrite(iisUrlRewriteStreamReader)
            .Add(MethodRules.RedirectXmlFileRequests)
            .Add(MethodRules.RewriteTextFileRequests)
            .Add(new RedirectImageRequests(".png", "/png-images"))
            .Add(new RedirectImageRequests(".jpg", "/jpg-images"));

        app.UseRewriter(options);
    }

    app.UseStaticFiles();

    app.Run(context => context.Response.WriteAsync(
        $"Rewritten or Redirected Url: " +
        $"{context.Request.Path + context.Request.QueryString}"));
}

RewriteRules.cs

public static void RewriteTextFileRequests(RewriteContext context)
{
    var request = context.HttpContext.Request;

    if (request.Path.Value.EndsWith(".txt", StringComparison.OrdinalIgnoreCase))
    {
        context.Result = RuleResult.SkipRemainingRules;
        request.Path = "/file.txt";
    }
}

基于 IRule 的规则

使用 Add 在实现 IRule 接口的类中使用规则逻辑。 与使用基于方法的规则方法相比,IRule 提供了更大的灵活性。 实现类可能包含构造函数,可在其中传入 ApplyRule 方法的参数。

public void Configure(IApplicationBuilder app)
{
    using (StreamReader apacheModRewriteStreamReader = 
        File.OpenText("ApacheModRewrite.txt"))
    using (StreamReader iisUrlRewriteStreamReader = 
        File.OpenText("IISUrlRewrite.xml")) 
    {
        var options = new RewriteOptions()
            .AddRedirect("redirect-rule/(.*)", "redirected/$1")
            .AddRewrite(@"^rewrite-rule/(\d+)/(\d+)", "rewritten?var1=$1&var2=$2", 
                skipRemainingRules: true)
            .AddApacheModRewrite(apacheModRewriteStreamReader)
            .AddIISUrlRewrite(iisUrlRewriteStreamReader)
            .Add(MethodRules.RedirectXmlFileRequests)
            .Add(MethodRules.RewriteTextFileRequests)
            .Add(new RedirectImageRequests(".png", "/png-images"))
            .Add(new RedirectImageRequests(".jpg", "/jpg-images"));

        app.UseRewriter(options);
    }

    app.UseStaticFiles();

    app.Run(context => context.Response.WriteAsync(
        $"Rewritten or Redirected Url: " +
        $"{context.Request.Path + context.Request.QueryString}"));
}

检查示例应用中 extensionnewPath 的参数值是否符合多个条件。 extension 必须包含一个值,并且该值必须是 .png.jpg 或 .gif。 如果 newPath 无效,则会引发 ArgumentException。 如果发出针对 image.png 的请求,请求将重定向到 /png-images/image.png。 如果发出针对 image.jpg 的请求,请求将重定向到 /jpg-images/image.jpg。 状态代码设置为 301 - Moved Permanentlycontext.Result 设置为停止处理规则并发送响应。

public class RedirectImageRequests : IRule
{
    private readonly string _extension;
    private readonly PathString _newPath;

    public RedirectImageRequests(string extension, string newPath)
    {
        if (string.IsNullOrEmpty(extension))
        {
            throw new ArgumentException(nameof(extension));
        }

        if (!Regex.IsMatch(extension, @"^\.(png|jpg|gif)$"))
        {
            throw new ArgumentException("Invalid extension", nameof(extension));
        }

        if (!Regex.IsMatch(newPath, @"(/[A-Za-z0-9]+)+?"))
        {
            throw new ArgumentException("Invalid path", nameof(newPath));
        }

        _extension = extension;
        _newPath = new PathString(newPath);
    }

    public void ApplyRule(RewriteContext context)
    {
        var request = context.HttpContext.Request;

        // Because we're redirecting back to the same app, stop 
        // processing if the request has already been redirected
        if (request.Path.StartsWithSegments(new PathString(_newPath)))
        {
            return;
        }

        if (request.Path.Value.EndsWith(_extension, StringComparison.OrdinalIgnoreCase))
        {
            var response = context.HttpContext.Response;
            response.StatusCode = (int) HttpStatusCode.MovedPermanently;
            context.Result = RuleResult.EndResponse;
            response.Headers[HeaderNames.Location] = 
                _newPath + request.Path + request.QueryString;
        }
    }
}

原始请求:/image.png

For image.png: Browser window with developer tools tracking the requests and responses

原始请求:/image.jpg

For image.jpg: Browser window with developer tools tracking the requests and responses

正则表达式示例

目标 正则表达式字符串和
匹配示例
替换字符串和
输出示例
将路径重写为查询字符串 ^path/(.*)/(.*)
/path/abc/123
path?var1=$1&var2=$2
/path?var1=abc&var2=123
去掉尾部反斜杠 (.*)/$
/path/
$1
/path
强制添加尾部反斜杠 (.*[^/])$
/path
$1/
/path/
避免重写特定请求 ^(.*)(?<!\.axd)$^(?!.*\.axd$)(.*)$
正确:/resource.htm
错误:/resource.axd
rewritten/$1
/rewritten/resource.htm
/resource.axd
重新排列 URL 段 path/(.*)/(.*)/(.*)
path/1/2/3
path/$3/$2/$1
path/3/2/1
替换 URL 段 ^(.*)/segment2/(.*)
/segment1/segment2/segment3
$1/replaced/$2
/segment1/replaced/segment3

其他资源