URL redirect and URL rewrite

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, and the redirect URL returned to the client will appear 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 will request /resource, and the server will respond 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 will execute a new request for the resource at the redirect URL.+

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.

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 is not returned to the client and won’t appear in a browser’s address bar. When /resource is rewritten to /different-resource, the client will request /resource, and the server will internally fetch 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.

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.

Use URL Rewriting Middleware when you are unable to use the URL Rewrite module in IIS on Windows Server, the Apache mod_rewrite module on Apache Server, URL rewriting on Nginx, or your application is hosted on WebListener server.┬áThe RewriteOptions are passed into the URL Rewriting Middleware as it’s added to the request pipeline with app.UseRewriter (Configure method in Startup.cs):

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);

To use IIS Rewrite module put rules into a different file (IISUrlRewrite.xml):

<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>

If you have an active IIS Rewrite Module with server-level rules configured that would impact your application in undesirable ways, you can disable the IIS Rewrite Module for an application with a change to your web.config file.

<configuration> 
 <system.webServer> 
 <modules> 
 <remove name="RewriteModule" /> 
 </modules> 
 </system.webServer> 
</configuration>