在javascript中将404重定向到类似页面

时间:2016-01-02 01:22:56

标签: javascript html url-redirection

在我的网站中,我希望能够重定向到任何将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距离做到这一点,但我不知道如何开始编码。

2 个答案:

答案 0 :(得分:2)

对于这些类型的任务,我喜欢使用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;
<div id="output"></div>

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

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

答案 1 :(得分:0)

按照此处所述创建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上托管,据我所知,您实际上没有服务器端控制。