I've made a static site using Next JS that is deployed on cloudfront. I'd like to have some routes redirect to another site by just changing the base path (e.g if the route is www.mysite.com/stuff, it will redirect to www.redirectedSite.com/stuff). Is this possible and if so, what would I use to set this up?
Asked
Active
Viewed 1,227 times
0
-
Does this answer your question: [Next.js Redirect from / to another page](https://stackoverflow.com/a/64198299/1870780)? You can use [`redirects`](https://nextjs.org/docs/api-reference/next.config.js/redirects) to do so. – juliomalves Dec 16 '21 at 18:32
1 Answers
1
Inside the getStaticProps
function, there is an option to return a redirect value. This allows for redirecting to internal and external resources.
You could have the code below saved under pages/stuff.tsx
. When a request is made to /stuff
, they will be redirected.
import { GetStaticProps } from 'next';
export const getStaticProps: GetStaticProps = async () => {
return {
redirect: {
destination: 'http://www.redirectedsite.com/stuff',
permanent: false,
},
};
};
const TestPage = (): JSX.Element => {
return <div></div>;
};
export default TestPage;

Shu Takahashi
- 46
- 4
-
This is awesome. Thank you very much for finding this! This solved my issue! – Draen Dec 20 '21 at 14:30