Adding FormerUrl's in code: how to generate the WebPageFormerUrlPathHash?
As a quick "redirection" module when moving from an old to a new site, I want to add FormerUrls to pages in the new site. That works perfectly, but I cannot find anywhere how to create the WebPageFormerUrlPathHash.
Since there is a difference in how Kentico 13.x created those, I cannot figure out how to calculate it now.
Of course I check if the old url is not already available as a page in the database. I skip those.
What method should I use? (hopefully not an internal one...)
Environment
Xperience by Kentico version: [30.8.0]
Execution environment: [Private cloud (Azure)]
Answers
I'm not sure how programmatic use of Former URLs is supposed to work - it might not be a supported API.
However, Vanity URLs have documented and public APIs for programmatic management, so I'd recommend using them instead.
Good point. We have two different imports, one of the old URL's, and one of marketing URL's. We wanted those marketing URL's as Vanity URL's anyway, but why not both?
Works the same, but there is no differentiation anymore.
It was a puzzle, but I found the solution:
easy one is use the stored procedure:
Proc_CMS_WebPageFormerUrlPath_InsertRedirect
Or use this method:
private static string ComputeUrlPathHash(string formerUrlPath)
{
using var sha256 = System.Security.Cryptography.SHA256.Create();
var inputBytes = Encoding.Unicode.GetBytes(formerUrlPath.ToLowerInvariant());
var hashBytes = sha256.ComputeHash(inputBytes);
return Convert.ToHexString(hashBytes);
}
To answer this question, you have to login first.