I currently have the following redirection on my website: example.com/[0-9]+/[0-9]+ → example.com/tag/$1-$2 where $1 and $2 are the values from the previous regex. I wrote this simple plugin below and it works perfectly if I type example.com/[0-9]+/[0-9]+ to the url bar. However, if I give the same link in a post inside discourse, the redirection doesn’t work. Is there a way to accomplish this?
Discourse::Application.routes.append do
get '/:year/:id', to: redirect('/tag/%{year}-%{id}') , constraints: {year: /[0-9]+/, id: /[0-9]+/}
end
My initial purpose to have that route is not related to imports. I would like to use example.com/123/456 (123 and 456 is arbitrary here as long as they are both [0-9]+) as a shortcut for example.com/tag/123-456 so it is not semantically “wrong” to use it.
However, the route doesn’t work for internal links (I need to refresh the website to make the route work), hence I need to detect each example.com/[0-9]+/[0-9]+ and change it to example.com/tag/[0-9]+-[0-9]+ which looks practically impossible to do without parsing each new post one by one.
Sorry. I missed that your issue is that you don’t like the route for tags. Why do you think it will be valuable to change that route? Are people going to be typing them by hand? They’d not it, because discourse will let them search and enter them.
Can you take a step back and say what problem you’re solving by taking “tag” out of the tag route?
The idea is that users can hot-swap preprint.com with mywebsite.com and land on the exact paper’s page that they were browsing on the preprint server. This was working fine until someone gave an internal link mywebsite.com/123/456 in a post and we realized that it is not redirecting and instead gives a 404. I am using Discourse for a very specific and targeted user base, hence, my usecase may sound strange.
Aha. Then I think you want a theme component that will rewrite those links when they are entered into a post. I’m not quite sure how to do that. One, that would require a bit of user training would be bbcode for those links. But I think a theme component could just hijack those links and rewrite them when they get baked.
After you mentioned the theme components, I was able to partially accomplish what I was looking for using the following component: Linkify words in post