#Hi

30 messages · Page 1 of 1 (latest)

verbal willow
#

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.

robust orbitBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

verbal willow
#

i am doing this

#

but its not working

verbal willow
#

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.

sage atlas
#

well you just said you want to redirect in the question

#

are you sure the condition matched in hostname === 'go' && path === '/'?

verbal willow
#

Yes

sage atlas
#

could you share the repo?

verbal willow
#

i haven't pushed it to the repo yet

sage atlas
#
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");
    }
  }
}
verbal willow
#

its working

#

was not before

#

so here i have given the exact url but how to do when it is deployed

sage atlas
#

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

verbal willow
#

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))

#

but it didn't worked

sage atlas
#

what is the value of domain ?

#

try with

domain=http://localhost:3000
verbal willow
#

i did this
not working. It just stopped working

#

its redirecting to go.localhost