5b26154803
* Update url-rewriting.md * Create project.json * Add files via upload * Create add_rewrite.png * Add files via upload * Update toc.md * Update index.md * Update url-rewriting.md * Add files via upload * Add figures for redirect and rewrite * Update images * Update url-rewriting.md * Update RewriteRule.cs * Update Startup.cs * Minor image update * Add additional resource * Alphabetize project.json sections * Group System usings * Create README.md * Move sample info into the README * Delete sample info (info moved to README) * Add console logging dep * Add console logging * Alphabetize * Create License.txt * Update index.md * Create url_redirect.png * Add files via upload * Create project.json * Add files via upload * Create README.md * Create url-rewriting.md * Update toc.md * Delete add_apache_mod_redirect.png * Delete add_iis_url_rewrite.png * Delete add_redirect.png * Delete add_redirect_jpg_requests.png * Delete add_redirect_png_requests.png * Delete add_redirect_to_https.png * Delete add_redirect_to_https_permanent.png * Delete add_redirect_xml_requests.png * Delete add_rewrite.png * Delete url_redirect.png * Delete url_rewrite.png * Delete ApacheModRewrite.txt * Delete IISUrlRewrite.xml * Delete License.txt * Delete README.md * Delete RewriteRule.cs * Delete Startup.cs * Delete global.json * Delete project.json * Delete testCert.pfx * Delete url-rewriting.md * Update toc.md * Add link to middleware doc * Update url-rewriting.md * Update RewriteRule.cs * Update Startup.cs * Swap redirect+rewrite images * React to review feedback * Minor update * Fix PathString * Fix PathString * Add unsupported IIS feature issue links * Add trackAllCaptures PR link * Update sample * Update sample * Update images * Fix missing fundamentals node on doc link * React to feedback * Revert images * Remove heading code style * React to feedback * Update title and middleware heading levels * Update section heading and remove paragraph |
||
---|---|---|
.. | ||
ApacheModRewrite.txt | ||
IISUrlRewrite.xml | ||
License.txt | ||
README.md | ||
RewriteRule.cs | ||
Startup.cs | ||
global.json | ||
project.json | ||
testCert.pfx |
README.md
ASP.NET Core URL Rewriting Sample
This sample illustrates usage of ASP.NET Core URL Rewriting Middleware. The application demonstrates URL redirect and URL rewriting options.
When running the sample, a response will be served that shows the rewritten or redirected URL when one of the rules is applied to a request URL.
Examples in this sample
AddRedirect("redirect-rule/(.*)", "$1")
- Success status code: 302 (Found)
- Example (redirect): /redirect-rule/{capture_group} to /redirected/{capture_group}
AddRewrite(@"^rewrite-rule/(\d+)/(\d+)", "rewritten?var1=$1&var2=$2", skipRemainingRules: true)
- Success status code: 200 (OK)
- Example (rewrite): /rewrite-rule/{capture_group_1}/{capture_group_2} to /rewritten?var1={capture_group_1}&var2={capture_group_2}
AddApacheModRewrite(env.ContentRootFileProvider, "ApacheModRewrite.txt")
- Success status code: 302 (Found)
- Example (redirect): /apache-mod-rules-redirect/{capture_group} to /redirected?id={capture_group}
AddIISUrlRewrite(env.ContentRootFileProvider, "IISUrlRewrite.xml")
- Success status code: 200 (OK)
- Example (rewrite): /iis-rules-rewrite/{capture_group} to /rewritten?id={capture_group}
Add(RedirectXMLRequests)
- Success status code: 301 (Moved Permanently)
- Example (redirect): /file.xml to /xmlfiles/file.xml
Add(new RedirectPNGRequests(".png", "/png-images")))
Add(new RedirectPNGRequests(".jpg", "/jpg-images")))
- Success status code: 301 (Moved Permanently)
- Example (redirect): /image.png to /png-images/image.png
- Example (redirect): /image.jpg to /jpg-images/image.jpg
Using a PhysicalFileProvider
You can also obtain an IFileProvider
by creating a PhysicalFileProvider
to pass into the AddApacheModRewrite()
and AddIISUrlRewrite()
methods:
using Microsoft.Extensions.FileProviders;
PhysicalFileProvider fileProvider = new PhysicalFileProvider(Directory.GetCurrentDirectory());
Secure redirection extensions
This sample includes WebHostBuilder
configuration for the app to use URLs (https://localhost:5001, https://localhost) and a test certificate (testCert.pfx) to assist you in exploring these redirect methods. Add any of them to the RewriteOptions()
in Startup.cs to study their behavior.
Method | Status Code | Port |
---|---|---|
.AddRedirectToHttpsPermanent() |
301 | null (465) |
.AddRedirectToHttps() |
302 | null (465) |
.AddRedirectToHttps(301) |
301 | null (465) |
.AddRedirectToHttps(301, 5001) |
301 | 5001 |