简体   繁体   English

如何重定向Node.js + Express中的嵌套URL

[英]How-to redirect nested urls in Node.js + Express

I'm tryin to do a module that redirect old urls to the new one on Node.JS + Express: 我正在尝试做一个模块,将旧网址重定向到Node.JS + Express上的新网址:

app.get('/category1', function (req, res){ res.redirect('/category2', 301) }) - works fine. app.get('/category1', function (req, res){ res.redirect('/category2', 301) }) - 工作正常。

but when we're goin to /category1/subcategory it doesnt redirect to /category2/subcategory 但是当我们进入/category1/subcategory它不会重定向到/category2/subcategory

How-to do this redirect rule like in apache's .htaccess: 如何在apache的.htaccess中执行此重定向规则:

RewriteRule ^category1/(.*)$ category2/$1 [R,NC,L]

eg 例如

/category1 -> /category2

/category1/ -> /category2/

/category1/2/3/ -> /category2/2/3/

/category1/2/?a=1&b=2 -> /category2/2/?a=1&b=2

You can't build a route like the following? 你不能建立如下的路线?

app.get('/category/*', function(req, res){ 
    res.redirect('/category2/' + req.params[0], 301);
});

Edited for wildcard use case. 编辑通配符用例。

I'd suggest checking out expressjs.com's section on routing. 我建议查看expressjs.com关于路由的部分。

http://expressjs.com/guide.html#routing http://expressjs.com/guide.html#routing

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM