add url mapping referrer

This commit is contained in:
sbwalker
2025-12-19 15:06:06 -05:00
parent 417a6bf226
commit 8120db84f4
5 changed files with 51 additions and 2 deletions

View File

@@ -294,8 +294,11 @@
private void HandlePageNotFound(Site site, Page page, Route route)
{
// referrer will only be set if the link originated externally
string referrer = (Context.Request.Headers[HeaderNames.Referer] != StringValues.Empty) ? Context.Request.Headers[HeaderNames.Referer] : "";
// page not found - look for url mapping
var urlMapping = UrlMappingRepository.GetUrlMapping(site.SiteId, route.PagePath);
var urlMapping = UrlMappingRepository.GetUrlMapping(site.SiteId, route.PagePath, referrer);
if (urlMapping != null && !string.IsNullOrEmpty(urlMapping.MappedUrl))
{
// redirect to mapped url

View File

@@ -0,0 +1,28 @@
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Oqtane.Databases.Interfaces;
using Oqtane.Migrations.EntityBuilders;
using Oqtane.Repository;
namespace Oqtane.Migrations.Tenant
{
[DbContext(typeof(TenantDBContext))]
[Migration("Tenant.10.00.02.03")]
public class AddUrlMappingReferrer : MultiDatabaseMigration
{
public AddUrlMappingReferrer(IDatabase database) : base(database)
{
}
protected override void Up(MigrationBuilder migrationBuilder)
{
var urlMappingEntityBuilder = new UrlMappingEntityBuilder(migrationBuilder, ActiveDatabase);
urlMappingEntityBuilder.AddStringColumn("Referrer", 2048);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
// not implemented
}
}
}

View File

@@ -14,6 +14,7 @@ namespace Oqtane.Repository
UrlMapping GetUrlMapping(int urlMappingId);
UrlMapping GetUrlMapping(int urlMappingId, bool tracking);
UrlMapping GetUrlMapping(int siteId, string url);
UrlMapping GetUrlMapping(int siteId, string url, string referrer);
void DeleteUrlMapping(int urlMappingId);
int DeleteUrlMappings(int siteId, int age);
}
@@ -78,6 +79,11 @@ namespace Oqtane.Repository
}
public UrlMapping GetUrlMapping(int siteId, string url)
{
return GetUrlMapping(siteId, url, "");
}
public UrlMapping GetUrlMapping(int siteId, string url, string referrer)
{
using var db = _dbContextFactory.CreateDbContext();
url = (url.StartsWith("/")) ? url.Substring(1) : url;
@@ -93,6 +99,7 @@ namespace Oqtane.Repository
urlMapping.Url = url;
urlMapping.MappedUrl = "";
urlMapping.Requests = 1;
urlMapping.Referrer = referrer;
urlMapping.CreatedOn = DateTime.UtcNow;
urlMapping.RequestedOn = DateTime.UtcNow;
try
@@ -109,6 +116,10 @@ namespace Oqtane.Repository
{
urlMapping.Requests += 1;
urlMapping.RequestedOn = DateTime.UtcNow;
if (!string.IsNullOrEmpty(referrer))
{
urlMapping.Referrer = referrer;
}
urlMapping = UpdateUrlMapping(urlMapping);
}
return urlMapping;