Middleware not working on production
Unanswered
British Shorthair posted this in #help-forum
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
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"
Can someone tell me why this isn't working and where is the mistake? Thanks!
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
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
where is your middleware.ts at?
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
British ShorthairOP
it's on the root level: /
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
do you use src folder
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
British ShorthairOP
no
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
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).*)',
]
}
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
British ShorthairOP
you mean the runtime logs?
because I actually added a console.log inside the if statement but nothing was showing up
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
maybe it doesn't match the if statement?
I added outside the if statement
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
British ShorthairOP
yeah I see
thanks
I will try to add it and will let you know later
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
British ShorthairOP
hi @Ray, I think I found where the problem is, apperantly the header
X-Forwarded-Port
is returning null![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
cool
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
British ShorthairOP
any idea how I can fix that?
Because I cannot find the header that returns the domain
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
where do you host your site
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
British ShorthairOP
on vercel
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
try
request.headers.get('Origin');
?![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
British ShorthairOP
ok
still return null
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
request.headers.get('x-forwarded-host')
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
British ShorthairOP
still it's not working
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
what does it return
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
British ShorthairOP
null
I tried url.host
from this
const url = request.nextUrl.clone();
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
console.log(Object.fromEntries(request.headers));
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
British ShorthairOP
I'll try that
url.host
is working
thanks a lot
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
no prob