Geta / geta-notfoundhandler

The popular NotFound handler for ASP.NET Core and Optimizely, enabling better control over your 404 page in addition to allowing redirects for old URLs that no longer works.
Apache License 2.0
19 stars 15 forks source link

Automatic Redirects: Trailing slash added to Old/New URLs #48

Open stkitzrow opened 1 year ago

stkitzrow commented 1 year ago

We are using the automatic redirects feature for a site, and it seems that a trailing slash is added to all the redirects, like so: image

It works great when the slash is present in the old url, but when using the old url without the trailing slash, we get a 404. Most users are accessing these URLs without a trailing slash, unless they're part of a path. Is there a way to tweak the behavior so that the old/new URLs can be added without this trailing slash? Even better, add two entries to account for both trailling slash & without?

marisks commented 1 year ago

Fixing trailing slashes is not really this library's purpose. You should be able to do it by implementing rewrite rules in ASP.NET Core: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/url-rewriting?view=aspnetcore-6.0&tabs=aspnetcore2x#regex-examples

stkitzrow commented 1 year ago

Thank you for your response @marisks,

I may be misunderstanding. I've already tried applying the "skip" trailing slash rule from that article. When I move a piece of content from within Optimizely, the automatic redirect still applies the '/' to the end of the new/old urls. I do not think I have any control over this?

I tried forcing our Opti .Net Core site to apply the trailing slash (to match the nuget package behavior), but that broke functionality on our site.

marisks commented 1 year ago

Those trailing slashes are not added by NotFoundHandler. Those URLs are what Optimizely resolves. Not sure how I can help.

GeekInTheNorth commented 1 year ago

The default routing options for URL generation in Optimizely CMS is to always add a trailing slash: https://docs.developers.optimizely.com/content-cloud/v12.0.0-content-cloud/docs/configuring-cms

I would suggest using a redirect rule to force the trailing slash on old URLs as this also aligns with SEO best practice. But do be careful, I got stung with special character encoding and a lowercase URL redirect ending up in an infinite loop. PathValue.Value is the original value, but PathValue.ToString() results in a re-encoding of special characters into upper case characters.