Rewrite external URLs

I enjoy using Discourse feature that automatically converts watched words into links. However, I’m also looking for a way to auto-convert external links, primarily to insert affiliate links (other than Amazon). I attempted to achieve this using the regex option in watched words, but it doesn’t seem to work for links. Does anyone have any suggestions?

Thank you !

Have you tried the Linkify words in post theme component? I think that has extra regex capabilities compared to Watched Words.

1 Like

Yes, but without success.

1 Like

The Discourse Affiliate plugin supports Amazon and the French site ldlc.com, if it’s something generally useful a PR is always welcome.

If not, you might use this plugin as a reference to what you want to accomplish.

3 Likes

Regexes are hard. Perhaps you should share what you tried?

1 Like

:100: :sweat_smile:

What I tried with the Google url example :

/(https?:\/\/)?(www\.)?google\.com/
/(https:\/\/)?(www\.)?((google)?\.com)/

1 Like

That’s not enough to debug. That will (should?) match a google URL, but what are you trying to replace?

I just need to match domain/subdomain, not all pages. Here is what I added in the linkify plugin :

/(https?:\/\/)?(www\.)?example\.com/, https://example.com/affiliateid

I’ve made some progress on my end, it seems that there could be a conflict with the enable markdown linkify setting.

This setting has the advantage over the component.

I’ll let Camille dig into it, but it may help resolve this issue

2 Likes

It looks that way to me too. I created a replacement for https://www.digitalocean.com/ → https://m.do.co/c/a5bf212d9a92/ and if enable markdown linkify is on, it doesn’t work, and if it’s off, it does. What I’d really like is for it to re-write the link.

A theme component might be a way to do that.

1 Like

Mmm so maybe the easiest solution is to fork the Discourse Affiliate plugin and add my custom affiliate links.

That’s likely overkill. If you just want to rewrite some urls, you don’t need much. I have a theme component that rewrites one boxes.

I have a theme component that finds one-boxes from a certain domain and rewrites them as <iframes> appropriate to that site. It shouldn’t be too hard (for someone with skills as meager as mine) to change it to re-write your links.

You pretty much need to change only this:

The main difference with an approach like Discourse Affiliate’s is that it changes the link in the cooked content, so it will also work outside the web application, like in emails or if using a different client (very, very rare). Just something to be aware of.

1 Like

Good point! And changing the cooked posts takes a plugin.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.