【问题标题】:redirect 404 to similar pages in javascript [closed]将404重定向到javascript中的类似页面[关闭]
【发布时间】:2016-01-02 01:22:55
【问题描述】:

在我的网站中,我希望能够重定向到任何出现 404 错误的用户,以重定向到与出现 404 错误的用户名称最接近(最相似)的页面。 例如,假设我有一个只有几页的简单网站。

var pages = ["index.html", "contact.html", "calander.html", 
"otherpage.html", "similarpage1.html" "similarpage2.html"]

当然,我还有一个标题为 404.html 的页面,这是我的 404 页面,但是 我希望这样当用户收到 404 错误时,他们可以单击一个按钮以重定向到推荐页面,即我上面描述的页面(该页面的名称与用户的页面最相似立即开启,产生了 404 错误。)


编辑:我只想使用客户端代码来执行此操作,因为我使用 github 页面来托管此网站,而且,如果标题不匹配任何内容(例如,如果有人请求 test.html),我希望它重定向到主页。 另外,知道我如何使用 levenshtein 距离来做到这一点,但我不知道如何真正开始编码。

【问题讨论】:

  • “最相似”是什么意思?如果有人请求test.html,你期望会发生什么?
  • 如果您只有几页,请不要打扰。如果您有很多页面,请使用服务器端代码来选择文件。
  • @user2182349 我的页面不止几页,但我只想使用服务器端代码
  • 您使用什么作为服务器堆栈?
  • 您的服务器端使用什么语言?如果是 javascript,您使用的是 nodejs 吗?

标签: javascript html url-redirection


【解决方案1】:

对于这些类型的任务,我喜欢使用levenshtein distance

Here's 一个 JavaScript 实现:

/*
Copyright (c) 2011 Andrei Mackenzie
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

// Compute the edit distance between the two given strings
function distance(a, b) {
  if (a.length == 0) return b.length;
  if (b.length == 0) return a.length;
  var matrix = [];

  // increment along the first column of each row
  var i;
  for (i = 0; i <= b.length; i++) {
    matrix[i] = [i];
  }

  // increment each column in the first row
  var j;
  for (j = 0; j <= a.length; j++) {
    matrix[0][j] = j;
  }

  // Fill in the rest of the matrix
  for (i = 1; i <= b.length; i++) {
    for (j = 1; j <= a.length; j++) {
      if (b.charAt(i - 1) == a.charAt(j - 1)) {
        matrix[i][j] = matrix[i - 1][j - 1];
      } else {
        matrix[i][j] = Math.min(matrix[i - 1][j - 1] + 1, // substitution
          Math.min(matrix[i][j - 1] + 1, // insertion
            matrix[i - 1][j] + 1)); // deletion
      }
    }
  }

  return matrix[b.length][a.length];
};


var pages = ["index.html", "contact.html", "calander.html",
  "otherpage.html", "similarpage1.html",
  "similarpage2.html"
];

function closest(page) {
  var smallest = -1;
  var rtn;
  for (var i = 0; i < pages.length; i++) {
    var dist = distance(page, pages[i]);
    if (dist < smallest || smallest == -1) {
      smallest = dist;
      rtn = pages[i];
    }
  }
  return rtn;
}

var test = ['indez.htm', 'contacts.html', 'calandar.htm', 'otherage.htm', 'simlarpag.htm', 'similarpage'];
var out = "";

for (var i = 0; i < test.length; i++)
  out += "Closest page for <b>" + test[i] + "</b> is <b>" + closest(test[i]) + "</b><br>";


document.getElementById("output").innerHTML = out;
&lt;div id="output"&gt;&lt;/div&gt;

现在您在 404.html 内,您可以使用 pageName 来确定要建议的页面:

document.write(
    "Couldn't find this page. Maybe you wanted " +
    closest(location.pathname) + "?");

【讨论】:

  • 这太棒了,我已经想到了概率选项,但认为它会很复杂,这很清楚很简单
  • 非常感谢。这正是我想要的。感谢您使它如此简单并提供示例。
【解决方案2】:

按照此处所述创建 404 页面:https://help.github.com/articles/custom-404-pages/

包含以下代码:

<script>

function mapToBestMatch(requestedPage) {
    var pages = ["index.html", "contact.html", "calander.html",
"otherpage.html", "similarpage1.html" "similarpage2.html"];

    // Logic to choose page that most closely matches the user's request
    return bestMatch;
}

location.href = mapToBestMatch(location.pathname);
</script>

虽然你说你想使用服务器端代码,但如果你在 github 上托管,据我了解,你并没有真正的服务器端控制。

【讨论】:

  • 我得到了这部分,我只是不知道逻辑部分该怎么做。这就是我问这个问题的原因
猜你喜欢
  • 2012-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-01
相关资源
最近更新 更多