diff --git a/CHANGELOG.md b/CHANGELOG.md index 56a64ff4..1532544b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file. ## [Unreleased] +### Added + +- Redirects can be set to *Exact match*, so a redirect from `/a` no longer also applies to `/a/b`. `/a/` and `/a?b=c` still match, as the query string isn't part of it. It's available on the Redirects tab, and in imported and exported CSV and XML files. Files without it still import, as a redirect that isn't exact. An import or an automatic redirect for an old url that's already saved replaces the row, so it clears the setting unless it sets it too, as it does with the wildcard setting and the redirect type. +- Each regex redirect can have its own redirect type. Rules saved earlier keep following `DefaultRedirectType` until you edit them, which saves the type shown on the form. + ### Security - Wildcard and regex redirects no longer let a request choose the host of the redirect. Before, `/content` -> `/` turned `/content//evil.com` into a redirect to `//evil.com`, and the regex format `/$1` did the same, as did `$1` capturing `https://evil.com`. A format that names a host itself, such as `//cdn.example.com/$1`, is left alone, and a capture naming the host of the request stays as it is. A rule that relied on `$1` capturing another host from the request now redirects to a path on the site. @@ -13,6 +18,9 @@ All notable changes to this project will be documented in this file. Check these when upgrading, as they change how existing rules and integrations behave: +- The database version goes from 2 to 3. At startup, the application adds `ExactMatch` to `NotFoundHandler.Redirects` and `RedirectType` to `NotFoundHandler.RegexRedirects`, so its database user needs permission to alter those tables. If that fails, an error is logged and reading the redirects then fails at startup. +- `IRegexRedirectsService` has new overloads of `Create` and `Update` taking a `RedirectType?`, so a class implementing it needs them too. The existing `Update` keeps the type of the rule. `RegexRedirectFactory.Create` and `CreateNew` and the `RegexRedirect` constructor take an optional `RedirectType?`, so code calling them needs recompiling, and code overriding the factory needs updating. +- The CSV export has a new `ExactMatch` column. - A redirect that would only append to itself now stops. With `/a` -> `/a/b`, `/a/x` still redirects to `/a/b/x`, but a request already under `/a/b` stays a 404. With `/a` -> `/a?x=1`, `/a?y=2` still redirects to `/a?y=2&x=1`, and that URL stays a 404. Before, these grew on every hop until the browser gave up. - URLs under an entry in the Deleted list now return `410`. Before, `/gone/`, `/gone?a=1` and `/gone/child` were redirected, often to the home page. - A request with no matching redirect keeps the status the application gave it. Before, a response that hadn't started yet was forced to `404`. `RequestHandler.SetStatusCodeAndShow404` is now only called for deleted URLs, so a subclass overriding it no longer runs for unmatched requests. @@ -33,6 +41,7 @@ Check these when upgrading, as they change how existing rules and integrations b ### Fixed +- Editing a regex redirect keeps its place in the order. Before, the edit form didn't send the order number, which moved the rule to the top, where it was tried first. - The admin pages confirm that a redirect was saved, deleted or ignored. Before, the page just reloaded. - A regex rule whose new url matches the rule again (`^/(.*)$` -> `/en/$1`) grows on every hop when the new url is also not found. diff --git a/README.md b/README.md index cfe02b02..405022da 100644 --- a/README.md +++ b/README.md @@ -155,7 +155,7 @@ If the `bufferSize` is set to `0`, the `threshold` value will be ignored, and ev **RedirectsRefreshInterval**: How often each server checks the database for redirect changes it hasn't picked up, in case an update never reached it. On Optimizely, servers normally hear about an update straight away through events, so this is a safety net. Without Optimizely it's how other servers pick up changes, so a new redirect can take up to this long to reach them. A server that reloads this way logs "Reloaded the redirects" at Information level, so on Optimizely, seeing that line regularly means update events aren't reaching it. Set to `TimeSpan.Zero` to turn the check off. Default is 5 minutes. (Ex. ```options.RedirectsRefreshInterval = TimeSpan.FromMinutes(1);```) -**DefaultRedirectType**: The redirect type pre-selected when adding a redirect, and used for redirects created from suggestions and by regex redirects. Default is `RedirectType.Temporary`. (Ex. ```options.DefaultRedirectType = RedirectType.Permanent;```) +**DefaultRedirectType**: The redirect type pre-selected when adding a redirect or a regex redirect, and used for redirects created from suggestions and for regex redirects saved before they had a type of their own. Default is `RedirectType.Temporary`. (Ex. ```options.DefaultRedirectType = RedirectType.Permanent;```) **ActiveStatusCodes**: A integerlist with the status codes that NotFoundHandler will be active on. (Ex. ```options.ActiveStatusCodes = new int[] { StatusCodes.Status404NotFound, StatusCodes.Status410Gone };```) @@ -349,7 +349,7 @@ Regex redirects are set up on the *Regex* tab, and are tried in order of their o ^https://www\.example\.com/en/(.*) -> /new/$1 ``` -Use the whole url to match only one host. A rule anchored at a `/` matches on every host of the site. A rule whose new url matches the rule again (`^/(.*)$` -> `/en/$1`) keeps redirecting if the new url isn't found either, so make sure it can't match its own result. +Use the whole url to match only one host. A rule anchored at a `/` matches on every host of the site. Each rule has its own redirect type, and rules saved before that was added use `DefaultRedirectType` until you edit them. A rule whose new url matches the rule again (`^/(.*)$` -> `/en/$1`) keeps redirecting if the new url isn't found either, so make sure it can't match its own result. ## Wildcards @@ -359,6 +359,16 @@ For example, if we have a redirect: `/a` to `/b`, then: - with wildcard setting it will redirect `/a/1` to `/b` - without wildcard setting it will redirect `/a/1` to `/b/1` +## Exact match + +By default, a redirect also applies to the urls below its old url, as described above. Tick *Exact match* on a redirect to only match the url itself. For example, with an exact match from `/a` to `/b`: +- `/a` and `/a/` redirect to `/b`, and `/a?c=d` redirects to `/b?c=d` +- `/a/1` isn't touched by this redirect, so it stays a 404 unless another redirect matches it + +The query string of the request isn't part of the exact match, so links with tracking parameters still redirect. An old url with a query string of its own needs those parameters in full, so `/a?x=1` matches `/a?x=1&y=2` but not `/a?x=12`. In a redirects file, use the `ExactMatch` column in a CSV file, and the `exactMatch="true"` attribute on `` in an XML file. + +## Redirect loops + A request is only redirected once by a rule that would match it again afterwards. For example, with a redirect from `/a` to `/a/b`, `/a/1` redirects to `/a/b/1`, and a request already under `/a/b` stays a 404. ## 🏁 Getting Started diff --git a/src/Geta.NotFoundHandler.Admin/Areas/GetaNotFoundHandlerAdmin/Pages/Index.cshtml b/src/Geta.NotFoundHandler.Admin/Areas/GetaNotFoundHandlerAdmin/Pages/Index.cshtml index 0446aead..ee7a8f8a 100644 --- a/src/Geta.NotFoundHandler.Admin/Areas/GetaNotFoundHandlerAdmin/Pages/Index.cshtml +++ b/src/Geta.NotFoundHandler.Admin/Areas/GetaNotFoundHandlerAdmin/Pages/Index.cshtml @@ -38,6 +38,9 @@ + + + @@ -58,6 +61,9 @@ + + + - } - else - { - - } + + + + +
+ +
+
diff --git a/src/Geta.NotFoundHandler.Admin/Areas/GetaNotFoundHandlerAdmin/Pages/Index.cshtml.cs b/src/Geta.NotFoundHandler.Admin/Areas/GetaNotFoundHandlerAdmin/Pages/Index.cshtml.cs index 089a4375..f2906e02 100644 --- a/src/Geta.NotFoundHandler.Admin/Areas/GetaNotFoundHandlerAdmin/Pages/Index.cshtml.cs +++ b/src/Geta.NotFoundHandler.Admin/Areas/GetaNotFoundHandlerAdmin/Pages/Index.cshtml.cs @@ -59,7 +59,10 @@ public IActionResult OnPostCreate() var customRedirect = new CustomRedirect(CustomRedirect.OldUrl, CustomRedirect.NewUrl, CustomRedirect.WildCardSkipAppend, - CustomRedirect.RedirectType); + CustomRedirect.RedirectType) + { + ExactMatch = CustomRedirect.ExactMatch + }; _redirectsService.AddOrUpdate(customRedirect); @@ -102,7 +105,8 @@ public IActionResult OnPostUpdate(RedirectsRequest request) OldUrl = EditRedirect.OldUrl, RedirectType = EditRedirect.RedirectType, NewUrl = EditRedirect.NewUrl, - WildCardSkipAppend = EditRedirect.WildCardSkipAppend + WildCardSkipAppend = EditRedirect.WildCardSkipAppend, + ExactMatch = EditRedirect.ExactMatch }); Confirmation = $"Saved the redirect from {EditRedirect.OldUrl} to {EditRedirect.NewUrl}."; diff --git a/src/Geta.NotFoundHandler.Admin/Areas/GetaNotFoundHandlerAdmin/Pages/Models/RedirectModel.cs b/src/Geta.NotFoundHandler.Admin/Areas/GetaNotFoundHandlerAdmin/Pages/Models/RedirectModel.cs index 229097e6..7b26588d 100644 --- a/src/Geta.NotFoundHandler.Admin/Areas/GetaNotFoundHandlerAdmin/Pages/Models/RedirectModel.cs +++ b/src/Geta.NotFoundHandler.Admin/Areas/GetaNotFoundHandlerAdmin/Pages/Models/RedirectModel.cs @@ -12,6 +12,7 @@ public class RedirectModel [Required] public string NewUrl { get; set; } public bool WildCardSkipAppend { get; set; } + public bool ExactMatch { get; set; } public RedirectType RedirectType { get; set; } } } diff --git a/src/Geta.NotFoundHandler.Admin/Areas/GetaNotFoundHandlerAdmin/Pages/Models/RegexRedirectModel.cs b/src/Geta.NotFoundHandler.Admin/Areas/GetaNotFoundHandlerAdmin/Pages/Models/RegexRedirectModel.cs index 08cb95e0..2f1c01b9 100644 --- a/src/Geta.NotFoundHandler.Admin/Areas/GetaNotFoundHandlerAdmin/Pages/Models/RegexRedirectModel.cs +++ b/src/Geta.NotFoundHandler.Admin/Areas/GetaNotFoundHandlerAdmin/Pages/Models/RegexRedirectModel.cs @@ -3,6 +3,7 @@ using System; using System.ComponentModel.DataAnnotations; +using Geta.NotFoundHandler.Core.Redirects; namespace Geta.NotFoundHandler.Admin.Pages.Geta.NotFoundHandler.Admin.Models; @@ -18,4 +19,6 @@ public class RegexRedirectModel [Required] [Display(Name = "Order Number")] public int OrderNumber { get; set; } + [Display(Name = "Redirect Type")] + public RedirectType? RedirectType { get; set; } } diff --git a/src/Geta.NotFoundHandler.Admin/Areas/GetaNotFoundHandlerAdmin/Pages/Regex.cshtml b/src/Geta.NotFoundHandler.Admin/Areas/GetaNotFoundHandlerAdmin/Pages/Regex.cshtml index aafced00..3cf32e2f 100644 --- a/src/Geta.NotFoundHandler.Admin/Areas/GetaNotFoundHandlerAdmin/Pages/Regex.cshtml +++ b/src/Geta.NotFoundHandler.Admin/Areas/GetaNotFoundHandlerAdmin/Pages/Regex.cshtml @@ -1,7 +1,11 @@ @page "{handler?}" @using Geta.NotFoundHandler.Core.Providers.RegexRedirects +@using Geta.NotFoundHandler.Core.Redirects +@using Geta.NotFoundHandler.Infrastructure.Configuration @using Microsoft.AspNetCore.Mvc.TagHelpers +@using Microsoft.Extensions.Options @model Geta.NotFoundHandler.Admin.Areas.Geta.NotFoundHandler.Admin.RegexModel +@inject IOptions Options; @await Component.InvokeAsync("Card", new { message = Model.Confirmation, cardType = CardType.Success }) @await Component.InvokeAsync("Card", new { message = Model.Message }) @@ -19,6 +23,9 @@ + + + @@ -38,6 +45,12 @@ + + +