您应该考虑在类别/部门表中创建一个表,其中每个类别都有唯一的 URL。然后您可以使用特殊例程来生成 URL。这可以是 SQL 标量函数,也可以是 CLR 函数,但它要做的事情之一就是标准化 Web 的 URL。您可以将“饮料和酒吧”转换为“饮料和酒吧”,将“糕点/装饰”转换为“糕点装饰”。主要是,该例程需要将所有无效的 HTTP URL 字符替换为其他字符。一个例子是这样的:
public static class URL
{
static readonly Regex feet = new Regex(@"([0-9]\s?)'([^'])", RegexOptions.Compiled);
static readonly Regex inch1 = new Regex(@"([0-9]\s?)''", RegexOptions.Compiled);
static readonly Regex inch2 = new Regex(@"([0-9]\s?)""", RegexOptions.Compiled);
static readonly Regex num = new Regex(@"#([0-9]+)", RegexOptions.Compiled);
static readonly Regex dollar = new Regex(@"[$]([0-9]+)", RegexOptions.Compiled);
static readonly Regex percent = new Regex(@"([0-9]+)%", RegexOptions.Compiled);
static readonly Regex sep = new Regex(@"[\s_/\\+:.]", RegexOptions.Compiled);
static readonly Regex empty = new Regex(@"[^-A-Za-z0-9]", RegexOptions.Compiled);
static readonly Regex extra = new Regex(@"[-]+", RegexOptions.Compiled);
public static string PrepareURL(string str)
{
str = str.Trim().ToLower();
str = str.Replace("&", "and");
str = feet.Replace(str, "$1-ft-");
str = inch1.Replace(str, "$1-in-");
str = inch2.Replace(str, "$1-in-");
str = num.Replace(str, "num-$1");
str = dollar.Replace(str, "$1-dollar-");
str = percent.Replace(str, "$1-percent-");
str = sep.Replace(str, "-");
str = empty.Replace(str, string.Empty);
str = extra.Replace(str, "-");
str = str.Trim('-');
return str;
}
}
您可以将此作为 SQL 增强功能,或将 URL 生成作为单独的进程运行。然后,要实现映射,您可以将整个 URL 直接映射到类别 ID。从长远来看,这种方法更好,原因有几个。首先,您并不总是生成网址,只需执行一次,它们就保持静态,您不必担心您的程序发生变化,然后 GoogleBot 无法找到旧网址。此外,如果发生冲突,您可能会注意到潜在的重复类别名称,因为冲突只会因特殊字符而有所不同。最后,您始终可以从数据库中查看 URL,而无需运行映射函数。