Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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.

Expand Down
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 };```)

Expand Down Expand Up @@ -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

Expand All @@ -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 `<old>` 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
<th class="col-1 text-center">
<vc:sortable-header-cell key="@nameof(CustomRedirect.WildCardSkipAppend)" display-name="Wildcard"/>
</th>
<th class="col-1 text-center">
<vc:sortable-header-cell key="@nameof(CustomRedirect.ExactMatch)" display-name="Exact match"/>
</th>
<th class="col-1">
<vc:sortable-header-cell key="@nameof(CustomRedirect.RedirectType)" display-name="Redirect Type"/>
</th>
Expand All @@ -58,6 +61,9 @@
<td class="text-center align-middle">
<input type="checkbox" class="form-check-input" asp-for="CustomRedirect.WildCardSkipAppend">
</td>
<td class="text-center align-middle">
<input type="checkbox" class="form-check-input" asp-for="CustomRedirect.ExactMatch">
</td>
<td>
<select class="form-select" asp-for="CustomRedirect.RedirectType">
<option value="@RedirectType.Temporary"
Expand Down Expand Up @@ -88,6 +94,9 @@
<td class="text-center align-middle">
@await Component.InvokeAsync("CheckboxReadonly", new { isChecked = item.WildCardSkipAppend })
</td>
<td class="text-center align-middle">
@await Component.InvokeAsync("CheckboxReadonly", new { isChecked = item.ExactMatch })
</td>
<td>@item.RedirectType</td>
<td>
@{
Expand Down Expand Up @@ -123,18 +132,18 @@
<div class="flex-fill">
<label>Wildcard</label>
<div class="input-group mb-2 text-center align-middle">
@if (item.WildCardSkipAppend)
{
<input type="checkbox" class="form-check-input"
asp-for="EditRedirect.WildCardSkipAppend" value="true"
checked="checked"/>
}
else
{
<input type="checkbox" class="form-check-input"
asp-for="EditRedirect.WildCardSkipAppend"
value="true"/>
}
<input type="checkbox" class="form-check-input"
asp-for="EditRedirect.WildCardSkipAppend"
checked="@item.WildCardSkipAppend"/>
</div>
</div>

<div class="flex-fill">
<label>Exact match</label>
<div class="input-group mb-2 text-center align-middle">
<input type="checkbox" class="form-check-input"
asp-for="EditRedirect.ExactMatch"
checked="@item.ExactMatch"/>
</div>
</div>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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}.";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.ComponentModel.DataAnnotations;
using Geta.NotFoundHandler.Core.Redirects;

namespace Geta.NotFoundHandler.Admin.Pages.Geta.NotFoundHandler.Admin.Models;

Expand All @@ -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; }
}
Original file line number Diff line number Diff line change
@@ -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<NotFoundHandlerOptions> Options;

@await Component.InvokeAsync("Card", new { message = Model.Confirmation, cardType = CardType.Success })
@await Component.InvokeAsync("Card", new { message = Model.Message })
Expand All @@ -19,6 +23,9 @@
<th>
<vc:sortable-header-cell key="@nameof(RegexRedirect.NewUrlFormat)" display-name="New URL Format"/>
</th>
<th class="col-1">
<vc:sortable-header-cell key="@nameof(RegexRedirect.RedirectType)" display-name="Redirect Type"/>
</th>
<th class="col-1"></th>
<th class="col-1"></th>
</tr>
Expand All @@ -38,6 +45,12 @@
<input required type="text" class="form-control" asp-for="RegexRedirect.NewUrlFormat">
<span asp-validation-for="RegexRedirect.NewUrlFormat" class="text-danger"></span>
</td>
<td>
<select class="form-select" asp-for="RegexRedirect.RedirectType" aria-label="Redirect Type">
<option value="@RedirectType.Temporary">@RedirectType.Temporary</option>
<option value="@RedirectType.Permanent">@RedirectType.Permanent</option>
</select>
</td>
<td>
<div class="d-grid gap-2">
<button type="submit" class="btn btn-primary"
Expand All @@ -52,10 +65,13 @@

@foreach (var item in Model.Items)
{
var redirectType = item.RedirectType ?? Options.Value.DefaultRedirectType;

<tr class="align-middle">
<td>@item.OrderNumber</td>
<td>@item.OldUrlRegex</td>
<td>@item.NewUrlFormat</td>
<td>@redirectType</td>
<td>
@{
var editModalId = $"editModal{item.Id}";
Expand Down Expand Up @@ -94,7 +110,20 @@
<span asp-validation-for="EditRedirect.NewUrlFormat"
class="text-danger"></span>

<div class="input-group mb-2">
<div class="input-group-prepend">
<span class="input-group-text">Redirect Type</span>
</div>
<select class="form-select" name="@Html.NameFor(m => m.EditRedirect.RedirectType)" aria-label="Redirect Type">
<option value="@RedirectType.Temporary"
selected="@(redirectType == RedirectType.Temporary)">@RedirectType.Temporary</option>
<option value="@RedirectType.Permanent"
selected="@(redirectType == RedirectType.Permanent)">@RedirectType.Permanent</option>
</select>
</div>

<input type="hidden" asp-for="EditRedirect.Id" value="@item.Id"/>
<input type="hidden" name="@Html.NameFor(m => m.EditRedirect.OrderNumber)" value="@item.OrderNumber"/>

<div class="d-block mt-3">
<button type="submit"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
using Geta.NotFoundHandler.Core.Providers.RegexRedirects;
using Geta.NotFoundHandler.Data;
using Geta.NotFoundHandler.Infrastructure;
using Geta.NotFoundHandler.Infrastructure.Configuration;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;

namespace Geta.NotFoundHandler.Admin.Areas.Geta.NotFoundHandler.Admin;

Expand All @@ -18,13 +20,16 @@ public class RegexModel : AbstractSortablePageModel
{
private readonly IRegexRedirectLoader _redirectLoader;
private readonly IRegexRedirectsService _regexRedirectsService;
private readonly NotFoundHandlerOptions _options;

public RegexModel(
IRegexRedirectsService regexRedirectsService,
IRegexRedirectLoader redirectLoader)
IRegexRedirectLoader redirectLoader,
IOptions<NotFoundHandlerOptions> options)
{
_regexRedirectsService = regexRedirectsService;
_redirectLoader = redirectLoader;
_options = options.Value;
}

public string Message { get; set; }
Expand All @@ -50,7 +55,10 @@ public IActionResult OnPostCreate()

if (ModelState.IsValid)
{
_regexRedirectsService.Create(RegexRedirect.OldUrlRegex, RegexRedirect.NewUrlFormat, RegexRedirect.OrderNumber);
_regexRedirectsService.Create(RegexRedirect.OldUrlRegex,
RegexRedirect.NewUrlFormat,
RegexRedirect.OrderNumber,
RegexRedirect.RedirectType);

Confirmation = $"Saved the regex redirect from {RegexRedirect.OldUrlRegex} to {RegexRedirect.NewUrlFormat}.";

Expand Down Expand Up @@ -80,7 +88,8 @@ public IActionResult OnPostUpdate()
_regexRedirectsService.Update(EditRedirect.Id.Value,
EditRedirect.OldUrlRegex,
EditRedirect.NewUrlFormat,
EditRedirect.OrderNumber);
EditRedirect.OrderNumber,
EditRedirect.RedirectType);

Confirmation = $"Saved the regex redirect from {EditRedirect.OldUrlRegex} to {EditRedirect.NewUrlFormat}.";

Expand All @@ -97,7 +106,11 @@ private void Load()
var items = FindRedirects();
Message = $"There are currently stored {items.Count()} Regex redirects.";
Items = items;
RegexRedirect = new RegexRedirectModel { OrderNumber = items.Select(x => x.OrderNumber).DefaultIfEmpty().Max() + 1 };
RegexRedirect = new RegexRedirectModel
{
OrderNumber = items.Select(x => x.OrderNumber).DefaultIfEmpty().Max() + 1,
RedirectType = _options.DefaultRedirectType
};
}

private IEnumerable<RegexRedirect> FindRedirects()
Expand Down
Loading
Loading