在node.js + express应用程序中,如何重定向URL以从先前站点中删除文件扩展名?

时间:2013-12-24 00:45:49

标签: javascript node.js .htaccess heroku express

我正在转换我们的网站,该网站位于ASP环境中并托管在Windows服务器上。

现有网址为

http://www.bruxzir.com/video-bruxzir-zirconia-dental-crown/index.aspx

但我需要它

http://www.bruxzir.com/video-bruxzir-zirconia-dental-crown/

我的app.js将此作为路线

app.get('/video-bruxzir-zirconia-dental-crown/', function(req, res){
  res.render('videos', {
    title: 'BruxZir Video Gallery'
  });
});

新网站可能会作为heroku上的node.js应用程序托管。我可以用.htaccess来处理这件事吗?如果是这样,这将被放置在快递应用程序中?并非所有现有网址都只有index.aspx个。

1 个答案:

答案 0 :(得分:0)

有很多方法可以让它发挥作用,但这里有一个带有一点中间件的方法:

app.use(function (req, res, next) {
  if (/index\.aspx$/i.test(req.path)) {
    res.redirect(req.path.slice(0, req.path.length - 11));
    return;
  }
  next();
});