So I want to have a subdomain named: “programare”, but I don’t know how to configure it well, I didn’t find really much documentation online about it too.
Context: a project in nextjs(app router, I will use it only for ssr), react, tailwind.
I want only “programare” to work as a subdomain, also what I did below is with the help of chatgpt, which for some reason it doesn’t seem right in my eyes.
(https://i.sstatic.net/kEIaioob.png)
Look in the picture for folders context
next.config.mjs
* @type {import('next').NextConfig}
*/
const nextConfig = {
images: {
domains: ["programare.localhost", "localhost"],
},
async rewrites() {
return [
{
source: "/",
destination: "/programare",
has: [
{
type: "host",
value: "programare.localhost:3000",
},
],
},
{
source: "/:path*",
destination: "/programare/:path*",
has: [
{
type: "host",
value: "programare.localhost:3000",
},
],
},
];
},
};
export default nextConfig;
middleware.js
import { NextResponse } from "next/server";
export function middleware(request) {
const { nextUrl, headers } = request;
const hostname = headers.get("host") || "";
const pathname = nextUrl.pathname;
if (
pathname.startsWith("/_next") ||
pathname.startsWith("/static") ||
pathname.startsWith("/assets")
) {
return NextResponse.next();
}
if (hostname === "programare.localhost:3000") {
if (pathname === "/") {
const url = nextUrl.clone();
url.pathname = "/programare";
return NextResponse.rewrite(url);
}
if (pathname.startsWith("/programare")) {
return NextResponse.next();
}
const url = nextUrl.clone();
url.pathname = `/programare${pathname}`;
return NextResponse.rewrite(url);
}
// Returnează o eroare 404 pentru toate celelalte subdomenii
if (hostname !== "localhost:3000") {
return NextResponse.error();
}
return NextResponse.next();
}
export const config = {
matcher: ["/:path*"],
};
For some reason chatgpt told me to modify hosts and add programare.localhost:3000 too in the txt file.
I want it to work in Production and Development too
I tried the chatgpt way but it gives me this error(pic), it also doesn’t see (https://i.sstatic.net/2G2YvgM6.png)m good to my eyes