如果给定的URL以斜杠结尾,如何重定向到基本URL?

时间:2017-05-24 15:30:44

标签: javascript node.js url-redirection

我有此链接http://localhost:3007/sellers-toolkit/,但右侧页面只是没有尾部斜杠的页面。所以我想重定向到http://localhost:3007/sellers-toolkit

我试图用JavaScript编写这个部分,但它只是让页面重新加载而没有任何反应。实现这种行为的正确方法是什么?

var url = "http://localhost:3007/sellers-toolkit/";
if (url) {
  url = "http://localhost:3007/sellers-toolkit";
  window.location.href = url;
}

2 个答案:

答案 0 :(得分:0)

我对这个问题的解决方案是我试图在javascript端做到这一点,但我发现我必须在我的节点服务器端执行此操作。

我使用了这段代码:

if (req.url == '/sellers-toolkit' || req.url == '/sellers-toolkit/' ) {
        category = 'Selling Privately';
    }

答案 1 :(得分:0)

尝试如下:

var url = "http://localhost:3007/sellers-toolkit/";
var spe_char_last =url.substr(url.length - 1);
if(spe_char_last == '/')
{
url = url.slice(0, -1);
if (url) {
  url = "http://localhost:3007/sellers-toolkit";
  window.location.href = url;
}
}