Home / how to redirect in iis / URL Rewriting Middleware in ASP.NET Core - Microsoft

URL Rewriting Middleware in ASP.NET Core - Microsoft - how to redirect in iis


URL Rewriting Middleware in ASP.NET Core - Microsoft-how to redirect in iis

URL Rewriting Middleware in ASP.NET
Core
By Luke Latham and Mikael Mengistu
View or download sample code (how to download)
URL rewriting is the act of modifying request URLs based on one or more predefined rules.
URL rewriting creates an abstraction between resource locations and their addresses so that the
locations and addresses are not tightly linked. There are several scenarios where URL rewriting
is valuable:
Moving or replacing server resources temporarily or permanently while maintaining
stable locators for those resources.
Splitting request processing across different apps or across areas of one app.
Removing, adding, or reorganizing URL segments on incoming requests.
Optimizing public URLs for Search Engine Optimization (SEO).
Permitting the use of friendly public URLs to help people predict the content they will
find by following a link.
Redirecting insecure requests to secure endpoints.
Preventing image hotlinking.
You can define rules for changing the URL in several ways, including Regex, Apache
mod_rewrite module rules, IIS Rewrite Module rules, and using custom rule logic. This
document introduces URL rewriting with instructions on how to use URL Rewriting Middleware
in ASP.NET Core apps.
Note
URL rewriting can reduce the performance of an app. Where feasible, you should limit the
number and complexity of rules.
URL redirect and URL rewrite
The difference in wording between URL redirect and URL rewrite may seem subtle at first but
has important implications for providing resources to clients. ASP.NET Core's URL Rewriting
Middleware is capable of meeting the need for both.
A URL redirect is a client-side operation, where the client is instructed to access a resource at
another address. This requires a round-trip to the server. The redirect URL returned to the client
appears in the browser's address bar when the client makes a new request for the resource.
If /resource is redirected to /different-resource, the client requests /resource. The server
responds that the client should obtain the resource at /different-resource with a status code
indicating that the redirect is either temporary or permanent. The client executes a new request
for the resource at the redirect URL.
When redirecting requests to a different URL, you indicate whether the redirect is permanent or
temporary. The 301 (Moved Permanently) status code is used where the resource has a new,
permanent URL and you wish to instruct the client that all future requests for the resource should
use the new URL. The client may cache the response when a 301 status code is received. The
302 (Found) status code is used where the redirection is temporary or generally subject to
change, such that the client shouldn't store and reuse the redirect URL in the future. For more
information, see RFC 2616: Status Code Definitions.
A URL rewrite is a server-side operation to provide a resource from a different resource address.
Rewriting a URL doesn't require a round-trip to the server. The rewritten URL isn't returned to
the client and won't appear in the browser's address bar. When /resource is rewritten to
/different-resource, the client requests /resource, and the server internally fetches the
resource at /different-resource. Although the client might be able to retrieve the resource at
the rewritten URL, the client won't be informed that the resource exists at the rewritten URL
when it makes its request and receives the response.
URL rewriting sample app
You can explore the features of the URL Rewriting Middleware with the URL rewriting sample
app. The app applies rewrite and redirect rules and shows the rewritten or redirected URL.
When to use URL Rewriting Middleware
Use URL Rewriting Middleware when you are unable to use the URL Rewrite module with IIS
on Windows Server, the Apache mod_rewrite module on Apache Server, URL rewriting on
Nginx, or your app is hosted on HTTP.sys server (formerly called WebListener). The main
reasons to use the server-based URL rewriting technologies in IIS, Apache, or Nginx are that the
middleware doesn't support the full features of these modules and the performance of the
middleware probably won't match that of the modules. However, there are some features of the
server modules that don't work with ASP.NET Core projects, such as the IsFile and
IsDirectory constraints of the IIS Rewrite module. In these scenarios, use the middleware
instead.
Package
To include the middleware in your project, add a reference to the
Microsoft.AspNetCore.Rewrite package. This feature is available for apps that target
ASP.NET Core 1.1 or later.
Extension and options
Establish your URL rewrite and redirect rules by creating an instance of the RewriteOptions
class with extension methods for each of your rules. Chain multiple rules in the order that you
would like them processed. The RewriteOptions are passed into the URL Rewriting
Middleware as it's added to the request pipeline with app.UseRewriter(options);.
C#
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
var options = new RewriteOptions()
.AddRedirect("redirect-rule/(.*)", "redirected/$1")
.AddRewrite(@"^rewrite-rule/(\d+)/(\d+)",
"rewritten?var1=$1&var2=$2",
skipRemainingRules: true)
.AddApacheModRewrite(env.ContentRootFileProvider,
"ApacheModRewrite.txt")
.AddIISUrlRewrite(env.ContentRootFileProvider, "IISUrlRewrite.xml")
.Add(RedirectXMLRequests)
.Add(new RedirectImageRequests(".png", "/png-images"))
.Add(new RedirectImageRequests(".jpg", "/jpg-images"));
app.UseRewriter(options);
}
URL redirect
Use AddRedirect to redirect requests. The first parameter contains your regex for matching on
the path of the incoming URL. The second parameter is the replacement string. The third

How does HTTP redirection in IIS work? HTTP Redirect module in IIS. It’s pretty straightforward. You give a target URL and set some options. I will be explaining what each option does through examples. You will see 3 options in “ Redirect Behavior ” section: Redirect all requests to exact destination (instead of relative to destination)