Redirect from non-existing url
# help-with-umbraco
h
Does anyone know of a way to redirect from a non existing url? We are making a new site and want the old url's to redirect to the new ones. We are using Umbraco 13.5.1 headless. Let's say todays website has an url "test.com/pages/about-us", but the url on the new site will be "test.com/about-us". We want this to redirect to the new url (e.g manual import). We have tried SEO toolkit redirect and Skybrud redirect, but the redirect only works from "api.test.com/pages/about-us" to "api.test.com/about-us" and not through the Content Delivery API through "api.test.com/umbraco/delivery/api/v2/content/item/pages/about-us", which only gives 404. Would we need to call the "api.test.com/pages/about-us" url for this to work or is there a way to make it work in the same way as the internal Umbraco redirects that are created when renaming a node?
a
In a headless setup, you have to handle this on your own. The last time I checked Umbraco doesn't support a way to handle manual redirects in the headless API. A solution could be that you create your own API, which if given a URL, returns whether a redirect exists at that URL - e.g. if you're using Skybrud Redirects, you can inject the
IRedirectsService
and use it to do the lookup on your own. So if you request the content API first, and get a 404, you can then send a request to your custom redirects API. Unfortunately this results in another API if the content isn't found in the first call, but it should do the trick.
h
Ah, I suspected as much. That solution could work for us. Another solution could be to get the frontend to call the Umbraco url directly to check for redirects if it gets a 404. "api.test.com/pages/about-us" seems to work as it does not go through the headless API. Would be an extra call no matter what we do
How would we implement that interface for the 404's from the delivery API? We tried using the IContentLastChanceFinder, but it does not trigger when the API returns 404
a
I don't have much experience with the headless APIs, so not sure I can help you much there. We have our own custom headless setup, so you request a single endpoint, and it then either responds with a content result or a redirect.
h
Ok. Thank you anyways. We'll check out the Redirects Service and try to intercept the 404
b
we have implemented something very similar what Anders is suggesting, by creating our own version of the Content Delivery API, so we could still use the rest of architecture provided by Umbraco. This also allowed us to extend some other parts of the content delivery api chain as well (ApiExtendedContentResponseBuilder logic mainly), although it obviously created extra work for any future version updates.
a
I'm also looking at this issue now - handling redirects in a headless. Are there any new ways to do it? Otherwise, I was thinking, umbraco has this IRequestRedirectService to handle it's own redirects. How about if I decorate IRequestRedirectService with my custom implementation which would also check with Skybrud IRedirectsService
24 Views