Problem
How to implement URL rewriting in ASP.NET Core.
Solution
In Startup
, configure RewriteOptions
and middleware for URL rewriting:
public void Configure(
IApplicationBuilder app,
IHostingEnvironment env)
{
var rewrite = new RewriteOptions()
.AddRedirect("films", "movies")
.AddRewrite("actors", "stars", true);
app.UseRewriter(rewrite);
app.Run(async context =>
{
var path = context.Request.Path;
var query = context.Request.QueryString;
await context.Response.WriteAsync($"New URL: {path}{query}");
});
}
Run and browse to /films, you’ll notice a redirect:
Browse to /actors, you’ll notice a rewrite:
Discussion
URL Rewriting is about modifying the request URL based on custom rules in order to decouple the address from the underlying resource. This could be for reasons like security, SEO, user friendly URL and redirect HTTP to HTTPS etc.
When you’re unable to use the rewriting features of web servers (IIS, Apache, Nginx), ASP.NET Core provides an alternate option, Response Rewrite middleware. It’s performance and feature set won’t match server based rewriting features though.
Rewrite middleware is capable of doing both redirects and rewrites:
Redirect
It’s a client-side operation and works like this:
- Client requests a resource e.g., /films
- Server responds with status code
301
(Moved Permanently
) or 302
(Found
) with new Location
header, instructing client to request the new location e.g. /movies
- Client makes request to new location, which will also be shown in the address bar.
Rewrite
It’s a server-side operation and works like this:
- Client requests a resource e.g. /actors
- Server will internally map to new location e.g. /stars and return
200
(OK
).
- Client will not know anything about this internal process and will still see the requested URL in address bar.
Rules
Redirect and Rewrite rules are Regular Expressions, details of which could be found in Useful Links section below.
Custom Rules
You could also create custom rules by creating a class and implementing IRule
interface:
public class MoviesRedirectRule : IRule
{
private readonly string[] matchPaths;
private readonly PathString newPath;
public MoviesRedirectRule(string[] matchPaths, string newPath)
{
this.matchPaths = matchPaths;
this.newPath = new PathString(newPath);
}
public void ApplyRule(RewriteContext context)
{
var request = context.HttpContext.Request;
if (request.Path.StartsWithSegments(new PathString(this.newPath)))
{
return;
}
if (this.matchPaths.Contains(request.Path.Value))
{
var newLocation = $"{this.newPath}{request.QueryString}";
var response = context.HttpContext.Response;
response.StatusCode = StatusCodes.Status302Found;
context.Result = RuleResult.EndResponse;
response.Headers[HeaderNames.Location] = newLocation;
}
}
}
Custom rule can be added to RewriteOptions
:
var rewrite = new RewriteOptions()
.Add(new MoviesRedirectRule(
matchPaths: new[] { "/films", "/features", "/albums" },
newPath: "/movies"));
Useful Link