forked from dotnet/AspNetDocs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
199 additions
and
199 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
aspnetcore/fundamentals/url-rewriting/sample/ApacheModRewrite.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
# Rewrite path with additional sub directory | ||
# Rewrite path with additional sub directory | ||
RewriteRule ^/apache-mod-rules-redirect/(.*) /redirected?id=$1 [L,R=302] |
14 changes: 7 additions & 7 deletions
14
aspnetcore/fundamentals/url-rewriting/sample/IISUrlRewrite.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
<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> | ||
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
Copyright (c) .NET Foundation. All rights reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use | ||
these files except in compliance with the License. You may obtain a copy of the | ||
License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software distributed | ||
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR | ||
CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
Copyright (c) .NET Foundation. All rights reserved. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use | ||
these files except in compliance with the License. You may obtain a copy of the | ||
License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software distributed | ||
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR | ||
CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
specific language governing permissions and limitations under the License. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,43 @@ | ||
# 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")))`<br>`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: | ||
```csharp | ||
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 | ||
# 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")))`<br>`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: | ||
```csharp | ||
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 |
116 changes: 58 additions & 58 deletions
116
aspnetcore/fundamentals/url-rewriting/sample/RewriteRule.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,59 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Text.RegularExpressions; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Rewrite; | ||
using Microsoft.Net.Http.Headers; | ||
|
||
namespace RewriteRules | ||
{ | ||
#region snippet1 | ||
public class RedirectImageRequests : IRule | ||
{ | ||
private readonly string _extension; | ||
private readonly PathString _newPath; | ||
|
||
public RedirectImageRequests(string extension, string newPath) | ||
{ | ||
if (string.IsNullOrEmpty(extension)) | ||
{ | ||
throw new ArgumentException(nameof(extension)); | ||
} | ||
|
||
if (!Regex.IsMatch(extension, @"^\.(png|jpg|gif)$")) | ||
{ | ||
throw new ArgumentException("The extension is not valid. The extension must be .png, .jpg, or .gif.", nameof(extension)); | ||
} | ||
|
||
if (!Regex.IsMatch(newPath, @"(/[A-Za-z0-9]+)+?")) | ||
{ | ||
throw new ArgumentException("The path is not valid. Provide an alphanumeric path that starts with a forward slash.", nameof(newPath)); | ||
} | ||
|
||
_extension = extension; | ||
_newPath = new PathString(newPath); | ||
} | ||
|
||
public void ApplyRule(RewriteContext context) | ||
{ | ||
var request = context.HttpContext.Request; | ||
|
||
// Because we're redirecting back to the same app, stop processing if the request has already been redirected | ||
if (request.Path.StartsWithSegments(new PathString(_newPath))) | ||
{ | ||
return; | ||
} | ||
|
||
if (request.Path.Value.EndsWith(_extension, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
var response = context.HttpContext.Response; | ||
response.StatusCode = StatusCodes.Status301MovedPermanently; | ||
context.Result = RuleResult.EndResponse; | ||
response.Headers[HeaderNames.Location] = _newPath + request.Path + request.QueryString; | ||
} | ||
} | ||
} | ||
#endregion | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Text.RegularExpressions; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Rewrite; | ||
using Microsoft.Net.Http.Headers; | ||
|
||
namespace RewriteRules | ||
{ | ||
#region snippet1 | ||
public class RedirectImageRequests : IRule | ||
{ | ||
private readonly string _extension; | ||
private readonly PathString _newPath; | ||
|
||
public RedirectImageRequests(string extension, string newPath) | ||
{ | ||
if (string.IsNullOrEmpty(extension)) | ||
{ | ||
throw new ArgumentException(nameof(extension)); | ||
} | ||
|
||
if (!Regex.IsMatch(extension, @"^\.(png|jpg|gif)$")) | ||
{ | ||
throw new ArgumentException("The extension is not valid. The extension must be .png, .jpg, or .gif.", nameof(extension)); | ||
} | ||
|
||
if (!Regex.IsMatch(newPath, @"(/[A-Za-z0-9]+)+?")) | ||
{ | ||
throw new ArgumentException("The path is not valid. Provide an alphanumeric path that starts with a forward slash.", nameof(newPath)); | ||
} | ||
|
||
_extension = extension; | ||
_newPath = new PathString(newPath); | ||
} | ||
|
||
public void ApplyRule(RewriteContext context) | ||
{ | ||
var request = context.HttpContext.Request; | ||
|
||
// Because we're redirecting back to the same app, stop processing if the request has already been redirected | ||
if (request.Path.StartsWithSegments(new PathString(_newPath))) | ||
{ | ||
return; | ||
} | ||
|
||
if (request.Path.Value.EndsWith(_extension, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
var response = context.HttpContext.Response; | ||
response.StatusCode = StatusCodes.Status301MovedPermanently; | ||
context.Result = RuleResult.EndResponse; | ||
response.Headers[HeaderNames.Location] = _newPath + request.Path + request.QueryString; | ||
} | ||
} | ||
} | ||
#endregion | ||
} |
Oops, something went wrong.