How to do 301 redirections

Thank you @christianoliveira, this helps me a lot! Just one question: I think I need to support those redirects, even when my new frontend with frontity is in place, so I somehow have to handle them in frontity itself. Where can I define them? Do I need a separate handler for them with the specific /number regex rule (think this is the only possibility?) Thanks!

Hi, @cobra! :wink:

So if you are deploying your site in now.sh you can use routes rules in your now.json file. Some like:

 {
  "version": 2,
  },
  "builds": [
    {
      "src": "package.json",
      "use": "@frontity/now"
    }
  ],
  "routes": [
    {
      "src": "/your-old-page",
      "status": 308,
      "headers": { "Location": "/your-new-page/" }
    }
  ]
}

Hope it will help you! :wink:

@cobra we haven’t finished the server extensibility yet but it’s high on our roadmap. Until that’s finished, you’d have to use redirections from your hosting like @christian.nascimento said. Which hosting are you using?

I think I’ll host with now.sh, but would like to be flexible with that as I currently have my own server. Do you have an idea how I could achieve the redirects in that case? I know it would be very hacky, but would that mean I have to implement the redirects on an upper level where I create the frontity server?

I’m not a JS nor regex expert, but could that be a solution unless redirects in frontity are possible?

var frontity = require("./build/server").default;
var http = require("http");

var server = http.createServer((req, res) => {
    //regex to match urls like (2020/02/02/my-article/2)
    const regex = /(.*\d+\/\d+\/\d+\/[a-z0-9]+(?:-[a-z0-9]+)*\/)(.*(\d+))$/;
    //hacky way to have 301 redirect.. (currently only for dev experiments ;))
    const match = req.url.match(regex);
    if (match) {
        res.writeHead(301, { Location: req.url.replace(regex, '$1') });
    }
});
server.on('request', frontity);

Not hacky at all, I think it’s a great idea :slight_smile:

You actually wrap the Frontity server in an Express or Koa app if you want. The server.js file exports a simple “req/res” function.