Next.js Discord

Discord Forum

Redirecting from www. to non www. address

Answered
Bavarian Mountain Hound posted this in #help-forum
Open in Discord
Avatar
Bavarian Mountain HoundOP
Hello guys,

I need to redirect my nextjs application from a www. address to a non www. address, but I'm not getting it to work.

I tried this approach:

{
source: "/:path",
has: [
{
type: "host",
value: "^www\."
}
],
destination: "https://:host/:path
",
permanent: true
}

But also a more direct approach:

{
source: "/:path",
has: [{ type: "host", value: "www.videoberatung.ch" }],
destination: "https://videoberatung.ch/:path
",
permanent: true
}

None of them seem to work. I don't have any way to debug this and a deployment takes some time. Does someone know how to correctly do this?

Greetings
Frederic
Answered by B33fb0n3
you can check this inside your middleware.ts. And then add a general matcher and check inside your code if the host starts with www. For example like this:
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
 
// This function can be marked `async` if using `await` inside
export function middleware(request: NextRequest) {
  const hostname = request.headers.get("host")!;
  if(hostname.startsWith("www.")) {
    // TODO: redirect to non www.
  }
}
 
// See "Matching Paths" below to learn more
export const config = {
  matcher: [
    /*
     * Match all request paths except for the ones starting with:
     * - api (API routes)
     * - _next/static (static files)
     * - _next/image (image optimization files)
     * - favicon.ico, sitemap.xml, robots.txt (metadata files)
     */
    '/((?!api|_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)',
  ],
}
View full answer

7 Replies

Avatar
you can check this inside your middleware.ts. And then add a general matcher and check inside your code if the host starts with www. For example like this:
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
 
// This function can be marked `async` if using `await` inside
export function middleware(request: NextRequest) {
  const hostname = request.headers.get("host")!;
  if(hostname.startsWith("www.")) {
    // TODO: redirect to non www.
  }
}
 
// See "Matching Paths" below to learn more
export const config = {
  matcher: [
    /*
     * Match all request paths except for the ones starting with:
     * - api (API routes)
     * - _next/static (static files)
     * - _next/image (image optimization files)
     * - favicon.ico, sitemap.xml, robots.txt (metadata files)
     */
    '/((?!api|_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)',
  ],
}
Answer
Avatar
Bavarian Mountain HoundOP
Okay, thank you. I'll try this.
I guess you would use somethin in the manner of return NextResponse.redirect(new URL('/login', request.url)) for redirecting?
Avatar
yea and make sure you using an absolute url to redirect away from www.
Avatar
@Bavarian Mountain Hound solved?
Avatar
Bavarian Mountain HoundOP
Yes and no, it didn't work out like this because the host header was not forwarded from the firewall. So in this header there was always the internal kubernetes address. For now I need to redirect on the client until we made the change to the firewall.
But thank you anyway, your answer helped me get to a working solution.