Skip to content

NextResponse redirect with Header Prop #51232

Discussion options

You must be logged in to vote

This is one of a way to do it.

import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
 
export function middleware(request: NextRequest) {
  if (request.nextUrl.pathname === '/') {
    const response = NextResponse.redirect(new URL('/test', request.nextUrl))
    response.headers.set('some-header', 'hello')

    return response
  }
}

Reference

Replies: 3 comments 6 replies

Comment options

You must be logged in to vote
5 replies
@SamKomesarook
Comment options

@IronBlossom
Comment options

@vicky-hemnani
Comment options

@LikeDreamwalker
Comment options

@lukeocodes
Comment options

Answer selected by SamKomesarook
Comment options

You must be logged in to vote
1 reply
@LikeDreamwalker
Comment options

Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
8 participants