How to implement URL rewriting in ASP.NET Core.
SolutionIn 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,

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, i.e., Response Rewrite middleware. Its performance and feature set won’t match the 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,
- The client requests a resource e.g. /films.
- The server responds with status code 301 (Moved Permanently) or 302 (Found) with new Location header, instructing a client to request the new location e.g. /movies.
- Client makes a request to the 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 the "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 already redirected, skip
- 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"));
Tridip BhattacharjeePosted Oct 13, 2017, 8:08 AM
Can we do self host asp.net mvc5 ?
Tridip BhattacharjeePosted Oct 13, 2017, 6:00 AM
Would like to recommend any best book which teach people from scratch asp.net core easy way.
Tridip BhattacharjeePosted Oct 13, 2017, 5:58 AM
Asp.net core can be self host? old version like asp.net mvc 5 can be self host? please guide me with right url on this topic. thanks
Tridip BhattacharjeePosted Oct 12, 2017, 5:40 AM
How to rewrite http to https with AddRedirect....any idea.
Tridip BhattacharjeePosted Oct 12, 2017, 5:39 AM
What is the meaning of these line of code taken from you one. app.Run(async context => { var path = context.Request.Path; var query = context.Request.QueryString; await context.Response.WriteAsync($"New URL: {path}{query}"); });
Talha Bin AfzalPosted Oct 12, 2017, 1:36 AM
Can you guide about the hosting of .Net core app?