301 Redirect with Node.js / Express.js - Tutorial with Example (stackfame.com)
const express = require('express');
const app = express();
const port = 3000;
app.get('/old-url/', (req, res, next) => {
res.redirect(301, 'https://new-doamin.com/new-url');
});
app.listen(port, () => {
console.log('Server is running at port: ',port);
});
and if you want force live request using express middleware use following code:
// forced 301 redirect using express middleware
const express = require('express');
const app = express();
const port = 3000;
app.use((req, res, next) => {
var host = req.get('Host');
if (host === 'www.old-domain.com') {
return res.redirect(301, 'https://new-domain.com/' + req.originalUrl);
}
return next();
});
app.listen(port, () => {
console.log('Server is running at port: ',port);
});
Không có nhận xét nào:
Đăng nhận xét