301 redirect when using SailsJs

浪尽此生 提交于 2019-12-24 03:16:19

问题


Situation: Upgrading an old site that was basic HTML to use SailsJS. Old site has .html extensions and is ranked well in search engines for these results. As such I would like to route all incoming requests from their old URLs (.html extension) to their new results generated via sails (no extension).

The best approach seems to be a 301 redirect but I am unsure as the best practices to implement this using a SailsJS based server.

www.blah.com/blah2.xml => www.blah.com/blah2.


回答1:


You should use Sails custom routes.


Copy your view file in the views folder. Replace its extension by ejs.

You can now add each route you want to use in your config/routes.js file like:

module.exports = {

    'GET /blah2.html': { view: 'blah2' },
    ...

};



回答2:


For SEO purposes you must handle this with HTTP status codes also. You should use custom routes (config/routes.js) and make a controller with this code:

module.exports = {
  // this is you action name
  redirect: function(req, res) {
    res.status(301);
    return res.view('blah2');
  }
}

You should also create the view in your views folder and set the route to go through your controller.




回答3:


I think what you are looking for is the redirect() function in sails. Because in terms of SEO, a 301 (permanent) redirect is the best practice.

Here you have an example how to use it inside your config/routes.js file:

'/blah2.xml': (req,res)=>{ return res.redirect(301,'/blah2') },

For more information refer to the sails.js documentation: https://sailsjs.com/documentation/reference/response-res/res-redirect



来源:https://stackoverflow.com/questions/30496874/301-redirect-when-using-sailsjs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!