Hi
Unanswered
Alligator mississippiensis posted this in #help-forum
Alligator mississippiensisOP
I want to redirect my subdomain to my main domain. I am not sure how to do that,. I used nextResponse.redirect() but its not doing anything.
For example - I want go.localhost to redirect to localhost.
Help me i am stuck at this point.
For example - I want go.localhost to redirect to localhost.
Help me i am stuck at this point.
29 Replies
@Alligator mississippiensis I want to redirect my subdomain to my main domain. I am not sure how to do that,. I used nextResponse.redirect() but its not doing anything.
For example - I want go.localhost to redirect to localhost.
Help me i am stuck at this point.
have you tried using
https://nextjs.org/docs/app/api-reference/next-config-js/redirects
redirects in next.config.js?https://nextjs.org/docs/app/api-reference/next-config-js/redirects
Alligator mississippiensisOP
i am doing this
but its not working
Alligator mississippiensisOP
What i want is that if there subdomain i will run a function and if i get the desired result then do some action else redirect to the main domain like. So that is why i am using middleware.
well you just said you want to redirect in the question
are you sure the condition matched in
hostname === 'go' && path === '/'?Alligator mississippiensisOP
Yes
@Alligator mississippiensis Yes
then it should work
could you share the repo?
Alligator mississippiensisOP
i haven't pushed it to the repo yet
@Alligator mississippiensis i haven't pushed it to the repo yet
I just did a quick test and it works
import { NextRequest, NextResponse } from "next/server";
export function middleware(req: NextRequest) {
const hostname = req.headers.get("host")?.split(".")[0];
const url = req.nextUrl.clone();
const path = url.pathname;
const domain = "";
if (hostname === domain) {
} else {
if (hostname === "go" && path === "/") {
return NextResponse.redirect("http://localhost:3000/p");
}
}
}Alligator mississippiensisOP
its working
was not before
so here i have given the exact url but how to do when it is deployed
if you are redirecting to same domain, you can do this
NextResponse.redirect(new URL('/home', req.url))otherwise create the domain in env variable
Alligator mississippiensisOP
domain is localhost and subdomain is go.localhost for testing
i have set domain to local host in env and imported the env file. but then i tried doing this
NextResponse.redirect(new URL('/home', domain))
NextResponse.redirect(new URL('/home', domain))
but it didn't worked
what is the value of domain ?
try with
domain=http://localhost:3000Alligator mississippiensisOP
i did this
not working. It just stopped working
not working. It just stopped working
Alligator mississippiensisOP
its redirecting to go.localhost
@Alligator mississippiensis Click to see attachment
try with
disable cache on chrome