Next.js Discord

Discord Forum

From id in url, to title

Unanswered
Pyr0Lover posted this in #help-forum
Open in Discord
I have a page /house/12345 as I need the id to make an api call, so it works quite nicely. But Im wondering, if I can create the same page, but have the title in the url, and have the canonical as this new one with the title inside the url

8 Replies

And if I pass a canonical tag, I should be able to keep my <a> tags as they are, sending to /house/id, while the browser will consider it the canocical, and actually show the /house/title to google? Meaning it would be little refactoring, but with SEO benefits? Im just not sure where to start
Sun bear
Cant you just make the api call with the title instead of the id?

You have to make sure that titles are unique in this case

/house/my-house-title

Then you can do

// const house = getHouseById(houseid)
const house = getHouseByTitle(houseTitle)
no they are not unique, and I also can't call the api with the title. I guess this means I will always need the id in the url?
@Pyr0Lover no they are not unique, and I also can't call the api with the title. I guess this means I will always need the id in the url?
Sun bear
Not sure. Maybe you can solve it with rewrites but not really sure how exactly to do it.
do you think if I add a url like /house/apartment-for-sale-in-new-york-12345 then I could parse the id, and perhaps still get some slight SEO benefits. But I guess it would not even be a noticable difference
Sun bear
I think you should check rewrites.

Maybe you can do something like:

/House/[id]/[title]

and rewrite to

/House/[title]

But not sure how exactly to do it.

The solution with title-id is not really perfect I would say. It will work of course but it could be better
I will take a look into rewrites, seems like it could work. Thank you!
Blue orchard bee
An option here is to create a SEO friendly hashed slug in the DB that references the article.

Instead of
/house/[id]/title
It would be
/house/(title-[hash]) <- string generated in DB