Let’s say my app is running on domain.com
and most of links in the internet to this site looks like this: domain.com/:id
.
I want to improve SEO a little bit and add redirects (301):
domain.com/:id => domain.com/:title
. But to get title for id I have to run angular app and check this in DB.
So what I want to do is:
- User go to
domain.com/:id
- Check
title
forid
from url inside angular app - Redirect user with 301 status to
domain.com/:title
2
Answers
You have to run Angular using Angular Universal with server-side rendering. There is a guide to get you started, and if it’s a new Angular project it’s relatively easy to add.
https://angular.io/guide/universal
You then have to tell Express to perform a 301 redirect, and this can be done by configuring the main module used for SSR.
https://gist.github.com/KostyaEsmukov/ce8a6486b2ea596c138770ae393b196f
You can make some changes or by adding a condition in your
server.ts
file.in the listed code, all routes first come here then the express server(SSR) render the front end angular HTML files. so before sending any response to the browser we can send the 301 status code.
we can add the condition like this
You can get more detail on this article