# Redirect Page for External URLs

**URL:** https://meta.discourse.org/t/redirect-page-for-external-urls/146798
**Category:** Support
**Created:** [April 4, 2020, 10:14am UTC](https://meta.discourse.org/t/redirect-page-for-external-urls/146798 "2020-04-04T10:14:21Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![CRUGG](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/crugg/32/161184_2.png) [@CRUGG](https://meta.discourse.org/u/CRUGG)
#### Post date: [April 4, 2020, 10:14am UTC](https://meta.discourse.org/t/redirect-page-for-external-urls/146798/1 "2020-04-04T10:14:21Z")

</div>

Hey there! I would like to redirect all external links (or if not possible to filter, all links) to e.g. `exit.website.tld/?page={{URL}}`  
Is this possible with Discourse? If not, is there a Plugin for that? I already searched but couldn’t find one with the search queries I used.  
So similar to how Steam does it. ([Example](https://steamcommunity.com/linkfilter/?url=https://meta.discourse.org))

---

<div class="post-metadata">

### Author: ![maiki](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/maiki/32/233950_2.png) [@maiki](https://meta.discourse.org/u/maiki)
#### Post date: [April 4, 2020, 7:11pm UTC](https://meta.discourse.org/t/redirect-page-for-external-urls/146798/2 "2020-04-04T19:11:11Z")

</div>

I had been thinking about this as well; I wanted to append the Internet Archive save URL to all external links, and let’s be honest: it would be cool to set by category.

I figure there is some filter thing to be done by a plugin, but folks do things with theme components that seem to bend the rules on data manipulation, so if this isn’t currently possible, how would folks approach adding it: plugin or theme component?

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [April 4, 2020, 9:49pm UTC](https://meta.discourse.org/t/redirect-page-for-external-urls/146798/3 "2020-04-04T21:49:15Z")

</div>

We view this as kind of a user-hostile feature to have (along the lines of “forum signatures”), so it’d need to be a plugin. Not sure if it could be done in a theme component?

I’ve seen this request before in the context of safety, as in

> 😱 oh noes! you’re about to visit a HYPERLINK to a WEBSITE! Have you prepared yourself for INCREDIBLE DANGER?!?!

and I _guess_ that could make sense in some extremely rare contexts in some extremely rare communities.. uh.. I guess?

---

<div class="post-metadata">

### Author: ![maiki](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/maiki/32/233950_2.png) [@maiki](https://meta.discourse.org/u/maiki)
#### Post date: [April 4, 2020, 11:36pm UTC](https://meta.discourse.org/t/redirect-page-for-external-urls/146798/4 "2020-04-04T23:36:37Z")

</div>

I run a category for archiving websites. Our use case is super-specific/lazy. 🙂

Also, I think of the web archive proxy as being similar to grabbing a copy of an image in a message, but again, _I run a category for archiving websites_. 🤓

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [April 5, 2020, 1:03am UTC](https://meta.discourse.org/t/redirect-page-for-external-urls/146798/5 "2020-04-05T01:03:36Z")

</div>

It’s fair, there may be some valid use cases, I’m just out here fighting for the users, man…

 ![OpenTerrificEquestrian-size_restricted](https://global.discourse-cdn.com/meta/original/3X/5/f/5f453b58b4bef4aa6ed4667d8c860fdbce7b8d9f.gif)

---

<div class="post-metadata">

### Author: ![CRUGG](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/crugg/32/161184_2.png) [@CRUGG](https://meta.discourse.org/u/CRUGG)
#### Post date: [April 5, 2020, 8:34am UTC](https://meta.discourse.org/t/redirect-page-for-external-urls/146798/6 "2020-04-05T08:34:07Z")

</div>

> [@codinghorror](#):
>
> I’ve seen this request before in the context of safety, as in
> 
> > 😱 oh noes! you’re about to visit a HYPERLINK to a WEBSITE! Have you prepared yourself for INCREDIBLE DANGER?!?!
> 
> and I _guess_ that could make sense in some extremely rare contexts in some extremely rare communities… uh… I guess?

In my opinion, it might actually be a safety thing, because of course here on the Discourse Meta Forum or on some Programming Forums people know how to differenciate URLs etc., but on Forums which aren’t so focussed on tech (especially Forums for non-tech Jobs, Schools etc.) many people might not notice if they’re on a site which looks the same but has a different TLD or maybe even a completely different Domain Name.  
And while this of course may be user-hostile for some Forums, nobody says it has to be enabled by Default. You could just make it like a check mark which would then use a Page by DIscourse itself or link a custom Page directly.  
In my opinion this is more of an essential feature which should be directly in Discourse, but of course, that’s just my own opinion.

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [April 5, 2020, 12:10pm UTC](https://meta.discourse.org/t/redirect-page-for-external-urls/146798/7 "2020-04-05T12:10:56Z")

</div>

Jeff has made it clear that this will need to be a plugin (unless it can be done in a theme, which seems likely). History suggests that if you develop the login and hundreds of sites want it, including dozens of enterprise customers, it should become an official plugin in only a few years.

But, if it’s possible to do in a theme, which I think it might be, then arguably discourse already supports the feature, you just need a theme to do it! You can check out [Developer’s guide to Discourse Themes](https://meta.discourse.org/t/developer-s-guide-to-discourse-themes/93648) or post in #marketplacewith a budget. My guess is that it’s at least $1000 but I could be wrong.

---

<div class="post-metadata">

### Author: ![CRUGG](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/crugg/32/161184_2.png) [@CRUGG](https://meta.discourse.org/u/CRUGG)
#### Post date: [April 5, 2020, 12:14pm UTC](https://meta.discourse.org/t/redirect-page-for-external-urls/146798/8 "2020-04-05T12:14:29Z")

</div>

> [@pfaffman](#):
>
> My guess is that it’s at least $1000 but I could be wrong.

While I don’t have experience with Discourse Themes etc., I have basic experience with JS and HTML. Something like this shouldn’t be more than 10-20 Minutes of Work. Why would that need a budget of $1000?

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [April 5, 2020, 12:19pm UTC](https://meta.discourse.org/t/redirect-page-for-external-urls/146798/9 "2020-04-05T12:19:35Z")

</div>

If you can do it in twenty minutes then you should have done it already. I’m still pretty bad at javascript and ember, maybe it’s much easier than I think. I think that it’s several hours work.

Please post a link to your theme! I can’t wait to see it.

---

<div class="post-metadata">

### Author: ![CRUGG](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/crugg/32/161184_2.png) [@CRUGG](https://meta.discourse.org/u/CRUGG)
#### Post date: [April 5, 2020, 12:36pm UTC](https://meta.discourse.org/t/redirect-page-for-external-urls/146798/10 "2020-04-05T12:36:39Z")

</div>

After around 5 Minutes I got working JS Code (which still needs some improvements, but it works; 10 lines by the way)  
The only thing I need to find out now is how to run this code after page load…

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [April 5, 2020, 1:32pm UTC](https://meta.discourse.org/t/redirect-page-for-external-urls/146798/11 "2020-04-05T13:32:37Z")

</div>

Awesome! Glad it was so easy! That stuff takes me an insane amount of time. I’m usually better as guessing how long it _should_ take.

---

<div class="post-metadata">

### Author: ![CRUGG](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/crugg/32/161184_2.png) [@CRUGG](https://meta.discourse.org/u/CRUGG)
#### Post date: [April 5, 2020, 1:37pm UTC](https://meta.discourse.org/t/redirect-page-for-external-urls/146798/12 "2020-04-05T13:37:36Z")

</div>

In general it’s just checking if you’re on an Article Page, looping through all `<article>` Tags, finding the actual Tag of the Content in it, looping through all `<a>` Tags in there and if their href isn’t an official site (using some basic RegExp for that) adding the Exit Page URL in front.  
But I’m still struggling at finding out how to run the JS Code **after** the Page has loaded and all dynamic content (such as Posts) have been loaded. A temporary solution would be to just run the Code after 1-2 seconds, but that isn’t that great and for some people it might take more than that to load…

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [April 6, 2020, 4:22am UTC](https://meta.discourse.org/t/redirect-page-for-external-urls/146798/13 "2020-04-06T04:22:52Z")

</div>

> [@pfaffman](#):
>
> Jeff has made it clear that this will need to be a plugin (unless it can be done in a theme, which seems likely).

It can certainly be done as a theme component, but keep in mind it will have to be a plugin if you want it to also affect the crawler view.

---

<div class="post-metadata">

### Author: ![Johani](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/johani/32/176920_2.png) [@Johani](https://meta.discourse.org/u/Johani)
#### Post date: [April 6, 2020, 7:43am UTC](https://meta.discourse.org/t/redirect-page-for-external-urls/146798/14 "2020-04-06T07:43:16Z")

</div>

> [@CRUGG](#):
>
> But I’m still struggling at finding out how to run the JS Code **after** the Page has loaded and all dynamic content (such as Posts) have been loaded. A temporary solution would be to just run the Code after 1-2 seconds, but that isn’t that great and for some people it might take more than that to load…

What you’re looking for is called a post decorator. It’s a hook that allows you to run scripts before each post is rendered. It’s a part of the [plugin API](https://github.com/discourse/discourse/blob/master/app/assets/javascripts/discourse/lib/plugin-api.js).

Now, regarding what you want to do (redirect all external links). I don’t think adding that much friction is a good idea, so I can’t help you with that - plus you already figured that bit out. That said, here’s a commented example of how you can create a setup that targets all external links in posts

This goes in the `header` section of your theme / theme component

```markdown
<script type="text/discourse-plugin" version="0.8">
  // store the hostname so we can reuse it.
  const siteHostname = location.hostname;
  
  // let's create a decorator to do this in every post
  api.decorateCooked(
    post => {
      // does the post have links?
      const links = [...post[0].querySelectorAll("a")];
      
      // no links, bail.
      if (!links.length) return;
      
      // We have links, let's filter them and only grab the ones that are external
      const externalLinks = links.filter(
        link => link.hostname !== siteHostname
      );
      
      // if we have some external links, let's do some work. For example, we can
      // add a class to each external link like so.
      externalLinks.forEach(link => {
        link.classList.add('external-link');
        // do more work here.
      });
    },
    // we give our decorator an id to prevent memory leaks.
    { id: "external-link-decorator" }
  );
</script>

```

Again, redirecting users is too much friction and will get annoying real fast. So, I would consider another approach, like maybe adding a subtle icon next to external links and teaching your users what it means instead?

 ![icons next to external links](https://global.discourse-cdn.com/meta/original/3X/f/2/f28ed837cf3855617e7f7195c5bcb849d07a73c0.png)

---

<div class="post-metadata">

### Author: ![riking](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/riking/32/170938_2.png) [@riking](https://meta.discourse.org/u/riking)
#### Post date: [April 9, 2020, 10:43pm UTC](https://meta.discourse.org/t/redirect-page-for-external-urls/146798/15 "2020-04-09T22:43:42Z")

</div>

> [@maiki](#):
>
> I run a category for archiving websites. Our use case is super-specific/lazy. 🙂
> 
> Also, I think of the web archive proxy as being similar to grabbing a copy of an image in a message, but again, _I run a category for archiving websites_ . 🤓

I think this use-case is much better served by a plugin or external integration (using a post create/edit webhook) that ensures all linked sites are properly preserved, whether or not someone clicks on them.

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [March 31, 2022, 8:30pm UTC](https://meta.discourse.org/t/redirect-page-for-external-urls/146798/16 "2022-03-31T20:30:18Z")

</div>

3 posts were split to a new topic: [Issue opening external links in new tab](https://meta.discourse.org/t/issue-opening-external-links-in-new-tab/222732)
