Next.js Discord

Discord Forum

Middleware not working on production

Unanswered
British Shorthair posted this in #help-forum
Open in Discord
British ShorthairOP
I have this middleware code, which checks if the user is on the main domain, if he isn't it redirects him to the main one. However this doesn't seem to be working. here is the code:
import { NextRequest, NextResponse } from "next/server"
export async function middleware(request: NextRequest) {
  const url = request.nextUrl.clone();

  const isProduction = process.env.NODE_ENV === "production";
  const requestedHost = request.headers.get("X-Forwarded-Port");

  if (isProduction && requestedHost && !requestedHost.match(/domain.com/)) {
    const host = `domain.com`;

    const requestedPort = request.headers.get('X-Forwarded-Port');
    const requestedProto = request.headers.get('X-Forwarded-Proto');

    url.host = host;
    url.protocol = requestedProto || url.protocol;
    url.port = requestedPort || url.port;

    return NextResponse.redirect(url);
  };

  return NextResponse.next();
}

export const config = {
  matcher: [
    '/((?!_next).*)',
  ]


Can someone tell me why this isn't working and where is the mistake? Thanks!

33 Replies

British ShorthairOP
it's on the root level: /
@British Shorthair it's on the root level: /
do you use src folder
British ShorthairOP
no
@British Shorthair no
try add a console.log and check the server console
export async function middleware(request: NextRequest) {
  const url = request.nextUrl.clone();

  const isProduction = process.env.NODE_ENV === "production";
  const requestedHost = request.headers.get("X-Forwarded-Port");

  console.log('middleware')

  if (isProduction && requestedHost && !requestedHost.match(/domain.com/)) {
    const host = `domain.com`;

    const requestedPort = request.headers.get('X-Forwarded-Port');
    const requestedProto = request.headers.get('X-Forwarded-Proto');

    url.host = host;
    url.protocol = requestedProto || url.protocol;
    url.port = requestedPort || url.port;

    return NextResponse.redirect(url);
  };

  return NextResponse.next();
}

export const config = {
  matcher: [
    '/((?!_next).*)',
  ]
}
British ShorthairOP
you mean the runtime logs?
because I actually added a console.log inside the if statement but nothing was showing up
British ShorthairOP
yeah I see
thanks
I will try to add it and will let you know later
British ShorthairOP
hi @Ray, I think I found where the problem is, apperantly the header X-Forwarded-Port is returning null
British ShorthairOP
any idea how I can fix that?
Because I cannot find the header that returns the domain
@British Shorthair any idea how I can fix that?
where do you host your site
British ShorthairOP
on vercel
@British Shorthair on vercel
try request.headers.get('Origin');?
British ShorthairOP
ok
still return null
@British Shorthair still return null
request.headers.get('x-forwarded-host')
British ShorthairOP
still it's not working
@British Shorthair still it's not working
what does it return
British ShorthairOP
null
I tried url.host
from this const url = request.nextUrl.clone();
@British Shorthair I tried url.host
console.log(Object.fromEntries(request.headers));
British ShorthairOP
I'll try that
url.host
is working
thanks a lot
no prob