Check If Url Exists in Umbraco Before Showing Page

Hello there. Within an existing non-Umbraco website, I would like to check if an Umbraco page exists (umbracoRedirectUrl table), and if so, redirect via 301 response to the page. If it doesn’t exist, deliver a non-Umbraco page within the website.

Any ideas anyone?

Before I just write a query to cache those urls and then check, I was wondering if there was some API call or Umbraco library I can use. Also, the non-Umbraco website is a old ASP.NET Webforms, and I’m running Umbraco 16. Different server machines but same database server.

Thank you!

Hi @MechaDev and welcome to the forum!

The umbracoRedirectUrl table will only contain Umbraco URLs that have been redirected, not all pages will be in this table.

I’d recommend not reading the database directly as I’m not aware of any guarantee these will be the same going forwards. Have you considered using the content delivery API for this purpose? Content Delivery API | Umbraco CMS

1 Like

Thanks for the reply. I was able to find/use Interface IPublishedUrlProvider | Umbraco c# Api docs

to help me put together a controller that would return all urls without going to the database directly.

Providing more context:

using var cref = _ctxFactory.EnsureUmbracoContext();
var ctx = cref.UmbracoContext;

var items = new List<object>(capacity: 2048);
long maxUpdatedTicks = 0;

foreach (var root in ctx.Content.GetAtRoot())
{
    foreach (var n in root.DescendantsOrSelf())
    {
        IEnumerable<string?> cultures = n.Cultures?.Keys ?? Enumerable.Empty<string>();
        if (!cultures.Any()) cultures = new[] { (string?)null }; // invariant

        foreach (var culture in cultures)
        {
            // In 16.1.1 the culture parameter is last
            var raw = culture == null
                ? _urlProvider.GetUrl(n, mode: absolute ? UrlMode.Absolute : UrlMode.Relative)
                : _urlProvider.GetUrl(n, mode: absolute ? UrlMode.Absolute : UrlMode.Relative, culture);

            // skip non-routable
            if (string.IsNullOrWhiteSpace(raw) || raw == "#")
                continue;

            var normalized = Normalize(raw, absolute);
            var updated = n.UpdateDate;

            if (updated.Ticks > maxUpdatedTicks) maxUpdatedTicks = updated.Ticks;
            
            items.Add(new
            {                            
                url = normalized,
                targetUrl = string.Empty
            });
        }
    }
}

Beware that this approach will cause all content to be eagerly loaded from the database into the cache. Depending on your cache configuration, how much content you’ve got etc. this might not be what you want. i.e. while it might not go to the database directly - you may find that the database usage from this approach works out higher overall.

You may find that combining the IDocumentUrlRepository.GetAll() method with your own caching layer, will give better, or at least more predictable performance.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.