简体   繁体   中英

URL rewriting using NodeJS

I want to customize my URL rewriting but it seems not working as I'd like it to.

My old code :

var rewrite = require('express-urlrewrite');

exports.rewrite = function(app){
  app.use(rewrite('/p/:id/:seoUrl', '/page/show/$1/$2'));
}

In my browser : http://mysite/p/1/seo-title (this URL works)

My new code :

var rewrite = require('express-urlrewrite');

exports.rewrite = function(app){
  app.use(rewrite('/:seoUrl', '/page/show/$1/$2'));
}

In my browser : http://mysite/seo-title (ID not found)

Using NodeJS, is there a way to exclude the ID in the URL?

well. if you take a close look at this: '/page/show/$1/$2' you might notice that it accepts two arguments.

since you removed :id you'd have to replace $1 with a static id or remove it completely aswell as you have to change $2 to $1

so in the end your code might look like this: app.use(rewrite('/:seoUrl', '/page/show/1/$1'));

or like this: app.use(rewrite('/:seoUrl', '/page/show/$1'));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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