Next.js redirect, rewrite 설정

모크장·2024년 4월 12일

next.config.mjs 파일에 설정

/** @type {import('next').NextConfig} */
const nextConfig = {
  async redirects(){
  return[
  	{
    source: 'products/temp',
    destination: 'products',
    permanent: true;
    },
  ];
 };
 };

export default nextConfig;

source로 접속했을 때 변경된 route를 destination에 기재, 영구적으로 변경됐는지 permanent true로 확인

redirect 되면 308코드를 받게 됨

const nextConfig = {
  async rewrites(){
  return[
  	{
    source: '/my',
    destination: 'me/info',
    },
    {
    source: '/items/:slug',
    destination: 'products/:slug',
    },
  ];
 };
 };

중요 정보를 포함한 route일 때 대체하거나 복잡한 구조를 간편한 url로 변경할 때 rewrite를 사용한다.

profile
시작하는 개발자

0개의 댓글