Topic URL to include its Category

I am investigating the possibility of including the category in the topic URL for retargeting purposes.

For example: https://meta.discourse.org/t/support/where-can-i-create-tags/31094 or https://meta.discourse.org/support/t/where-can-i-create-tags/31094

From previous responses on this subject, it seems as though this will wreck havoc on the routing system, correct?

What about the possibility of including the category in query string? https://meta.discourse.org/t/where-can-i-create-tags/31094/?category=support

Thoughts, suggestions, answers?

1 Like

I think if you put it before the topic ID at the end there’s no problem… but you can’t add more slashes or other delimiters.

Maybe you can use underscore to delimit if you need it, e.g. these two links go to the same place without any problem:

https://meta.discourse.org/t/topic-url-to-include-its-category/39996

https://meta.discourse.org/t/_support_topic-url-to-include-its-category/39996

your URL with question mark at the end might work - seems to work here. Not sure what the effect of that is.

https://meta.discourse.org/t/where-can-i-create-tags/31094/?category=support
3 Likes

Is this possible through a site setting, or a plugin?

Hi,

I’m very much a n00b when it comes to Discourse but also forums in general and I’d like to ask and understand if and how it is possible for a URL of a thread to contain it’s category.

My meaning being right now it appears I have a URL of a category like this:
https://randomname.discourse.group/c/category-name/6

But when clicking on a thread within that category I have a URL that contains no reference to the category:
https://randomname.discourse.group/t/what-a-great-thread/18

Apologies but I’ve tried to search and understand the answer, but I’ve not had much luck.

Help would be much appreciated.

Many thanks

A good idea to get the nomenclature right. In Discourse a thread is called a Topic.

The association with a Category is implicit so it’s not needed.

Plus if the Topic is moved to a new Category, the URL would become dated and essentially invalid.

There’s usually no harm in adding a query string parameter if you must.

But what’s your use case? What are you trying to achieve?

2 Likes

I’m utilising a third party software which scraps a forum. I’m limited to its queries and so when it scrapes a thread (topic) it needs to infer what category it is part of, which it appears to do via the url.

It makes no sense from an app perspective as I’ve pointed out, but you might be able to build a plugin or script to change all the URLs.

The Category is clear in the HTML of the crawler views I believe so maybe you can alter the tool instead to leverage that instead?

2 Likes

If you’re the one explicitly providing the URLs to the scraper and defining how it’ll extract the category from them, you might be able to use what Tobias suggested as a hack – you could provide it with /t/category-name__topic-slug/topic-id, for example. Discourse will serve a 301 redirect response to the final URL, based solely on the topic-id.

If it gets the URLs during the scraping process, though, it feels like a lot of work for a change aimed only at fulfilling a limitation in the scraping strategy.

Not that I know of. Serving a /t/category-name/topic-slug/topic_id route in a plugin seems doable, but then having an option so all references to topics in Discourse prefer this alternative route seems (to me, personally) like too much work for little gain.

2 Likes

Just a thought but have you considered using RSS feeds? Discourse has built in RSS feed output.

2 Likes

What software is it? You can get the category in a few ways without changing URLs (in the HTML and JSON).

For JSON, instead of scraping this:
https://meta.discourse.org/t/topic-url-to-include-its-category/39996

scrape this:
https://meta.discourse.org/t/topic-url-to-include-its-category/39996.json

and look for the category_id field.

Another possibility is using Data Explorer to extract what you need.

4 Likes