Next.js Discord

Discord Forum

generateStaticParams: hide original URL (only show rewritten URL)

Unanswered
Spectacled bear posted this in #help-forum
Open in Discord
Spectacled bearOP
I generate many detail pages via SSR.
Then I use middleware to generate SEO friendly URLs.
Problem: Google finds both versions and marks several of them as duplicate content.

## questions
1. How to hide the original URLs (which are using the SEO-unfriendly ID) from Google?
2. Or can I get rid of the original (ID-)URLs entirely, while keeping the SEO friendly URL structure?

Folder structure in Next.js:
src/app/heilpraktiker/[hpId]

SEO friendly URL example:
/heilpraktiker-john-doe-in-manhattan

I generate many detail pages via SSR like this:
export const generateStaticParams = async ({params}) => {
  return slugDb.map(hp => ({hpId: hp.id.toString()}))
};


Then I use middleware to generate SEO friendly URLs like this:
export async function middleware(request) {
    if (
        request.nextUrl.pathname.includes("/heilpraktiker-")
        && request.nextUrl.pathname.split('-').at(1) !== 'in'
        && request.nextUrl.pathname.split('-').includes('in')
    ) {
        const hpInCitySlugFromUrl = request.nextUrl.pathname.split("/heilpraktiker-").at(-1);

        const allHpData = await getAllHpData()
        const detailHp = allHpData.find(hp => `${hp.hpNameSlug}-in-${hp.hpCitySlug}` === hpInCitySlugFromUrl)

        return NextResponse.rewrite(new URL(`/heilpraktiker/${detailHp.id}`, request.url))
    }
}

1 Reply

Spectacled bearOP