How to redirect 301 in Angular Universal
Posted on February 27, 2022
To remove the old data from the search engine you need to add 301 redirects to your old pages.
The below method is used to redirect your old page to a new page using 301 redirects in Angular Universal.
This is by default code in server.ts
file
// All regular routes use the Universal engine
server.get('*', (req, res) => {
res.render(indexHtml, { req, providers: [{ provide: APP_BASE_HREF, useValue: req.baseUrl }] });
});
All routes come here then the express server(SSR) renders the front-end angular HTML files. So before sending any response to the browser we can send the 301 status code.
You can add some conditions like below
// All regular routes use the Universal engine
server.get('*', (req, res) => {
if (index == req.originalUrl)
res.redirect(301, 'https://www.samprix.com');
else
res.render(indexHtml, { req, providers: [{ provide: APP_BASE_HREF, useValue: req.baseUrl }] });
});
to join this conversation on Samprix.Already have an account? Sign in to comment