301 Redirect of web site

301 Redirect of web site

I had created a simple nodejs script to implement 301 redirects of a website. Three hundred-one (301) redirections are needed when a website name is changed. The old website should respond in a message like Moved Permanently. Redirecting to https:://new.web.addr . It is meant for the search engines to maintain the website credit. I have used heroku to host the script. Here is how to create a simple redirection service:

  1. create application - reffer here:
heroku create <app-name> 
git clone https://git.heroku.com/<app-name>.git
cd <app-name>
heroku info # it will give info about app , like url and git location:
  1. create app.js
const express = require('express');
const app = express();
const port = process.env.PORT || 5000
   if(req.headers['x-forwarded-proto']=='https' ||   req.headers['x-forwarded-proto']=='http'  )
     next() /* Continue to other routes if we're not redirecting */
app.listen(port, () => console.log(`listening  port ${port}!`))    
  1. run the following:
npm init
npn install express
  1. commit & push
git add app.js package.json
git push

To test the result, click on https://myredirect301.herokuapp.com/2021/05/301-redirect-of-web-site/ and it will lead you directly to this article.


[1] https://stackoverflow.com/questions/11744975/enabling-https-on-express-js
[2] https://stackfame.com/301-redirect-node-express
[3] https://nodejs.org/en/knowledge/HTTP/servers/how-to-create-a-HTTPS-server/
[4] https://timonweb.com/javascript/running-expressjs-server-over-https
[5] 301 redirect for site hosted at github
[6] https://gist.github.com/domenic/1f286d415559b56d725bee51a62c24a7
[7] usefull redirect template

comments powered by Disqus