Skip to content

Commit 94b3a0d

Browse files
committed
Enforce canonical redirect
Search engines and many analytic tools use case sensitive URLs, and can get fussy about inconsistencies in casing. Best case, this can complicate reporting by having multiple versions of the same page; worst case, it can impact performance due to competing content at two different URLs. The `GetWebPath()` URL is already used for the `SitemapController`, and most implementations also use it for the canonical URL in their HTML metatags. Establishing a redirect early on helps ensure that indexes only maintain the canonical URL. There may be scenarios where this would be better handled as an option, particularly for well-established sites who may have had a casing mismatch with their canonical URL, as redirecting a large number of pages simultaneously can cause a temporary hit to SEO. As we have high visibility into current implementations of OnTopic, that's not an immediate concern, though, and if it is we can revisit making this configurable. For now, however, we don't have a standard way of relaying configuration variables to e.g. attributes, so I am reluctant to implement a fix that relies on an ad hoc method of establishing such preferences. In a future release of OnTopic, we'll likely standard how these types of preferences can be stored and retrieved from the `ITopicRepository` (e.g., via a `GetConfigurationValue()` method or perhaps an extension method on `Topic`) and can reevaluate this then.
1 parent 69dff49 commit 94b3a0d

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

OnTopic.AspNetCore.Mvc/ValidateTopicAttribute.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,18 @@ public override void OnActionExecuting(ActionExecutingContext filterContext) {
140140
return;
141141
}
142142

143+
/*------------------------------------------------------------------------------------------------------------------------
144+
| Handle canonical URL
145+
>-------------------------------------------------------------------------------------------------------------------------
146+
| Most search engines are case sensitive, even though many web servers are configured case insensitive. To help avoid
147+
| mismatches between the requested URL and the canonical URL, and to help ensure that references to topics maintain the
148+
| same case as assigned in the topic graph, URLs that vary only by case will be redirected to the expected case.
149+
\-----------------------------------------------------------------------------------------------------------------------*/
150+
if (!currentTopic.GetWebPath().Equals(filterContext.HttpContext.Request.Path, StringComparison.Ordinal)) {
151+
filterContext.Result = controller.RedirectPermanent(currentTopic.GetWebPath());
152+
return;
153+
}
154+
143155
/*------------------------------------------------------------------------------------------------------------------------
144156
| Base processing
145157
\-----------------------------------------------------------------------------------------------------------------------*/

0 commit comments

Comments
 (0)