# Redirect Category to External URL

**URL:** https://meta.discourse.org/t/redirect-category-to-external-url/85691
**Category:** Support
**Created:** [4월 19, 2018, 5:32오전 UTC](https://meta.discourse.org/t/redirect-category-to-external-url/85691 "2018-04-19T05:32:29Z")
**Posts on this page:** 18
**Page:** 1

<div class="post-metadata">

### Author: ![Zyniker](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zyniker/32/120004_2.png) [@Zyniker](https://meta.discourse.org/u/Zyniker)
#### Post date: [4월 19, 2018, 5:32오전 UTC](https://meta.discourse.org/t/redirect-category-to-external-url/85691/1 "2018-04-19T05:32:29Z")

</div>

Is there some native facility for redirecting categories to external URLs?

Customize \>\> Permalinks apparently does _not_ support this use.

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [4월 19, 2018, 7:57오전 UTC](https://meta.discourse.org/t/redirect-category-to-external-url/85691/2 "2018-04-19T07:57:34Z")

</div>

Why would you redirect a category to an external URL?  
What’s the point of having the category if you only need a link?

---

<div class="post-metadata">

### Author: ![Zyniker](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zyniker/32/120004_2.png) [@Zyniker](https://meta.discourse.org/u/Zyniker)
#### Post date: [4월 19, 2018, 5:46오후 UTC](https://meta.discourse.org/t/redirect-category-to-external-url/85691/3 "2018-04-19T17:46:16Z")

</div>

To have the link displayed as a category without going through all the necessary CSS fiddling to do so without it simply _being_ a category.

The community where I’ll be doing this has a few related Discourse installs that each deal with a different set of topics; I want to more cleanly link them together by creating ‘categories’ that link from each site to each other site.

---

<div class="post-metadata">

### Author: ![Karl\_Romanowski](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/karl_romanowski/32/87676_2.png) [@Karl\_Romanowski](https://meta.discourse.org/u/Karl_Romanowski)
#### Post date: [4월 20, 2018, 1:02오전 UTC](https://meta.discourse.org/t/redirect-category-to-external-url/85691/4 "2018-04-20T01:02:38Z")

</div>

I briefly looked at doing this and it came down to two alternatives.

1. Create the category with read only permissions for everyone and put the link into the ‘About this category’ page, maybe including a short paragraph why, like an ‘official’ external support site.

2. Target and overwrite the link href in a theme component.

---

<div class="post-metadata">

### Author: ![Zyniker](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zyniker/32/120004_2.png) [@Zyniker](https://meta.discourse.org/u/Zyniker)
#### Post date: [4월 20, 2018, 1:05오전 UTC](https://meta.discourse.org/t/redirect-category-to-external-url/85691/5 "2018-04-20T01:05:05Z")

</div>

I was afraid it would come down to that or doing a rewrite at the server level. I think I may just go with the (somewhat questionable) JavaScript option.

---

<div class="post-metadata">

### Author: ![Zyniker](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zyniker/32/120004_2.png) [@Zyniker](https://meta.discourse.org/u/Zyniker)
#### Post date: [4월 20, 2018, 3:51오전 UTC](https://meta.discourse.org/t/redirect-category-to-external-url/85691/6 "2018-04-20T03:51:38Z")

</div>

So, related issue: I’ve now implemented a redirect (using JavaScript), but… it works only on refresh, which is clearly suboptimal. At any rate, here’s the code I’m using:

```plaintext
$( document ).ready(function() {
	if ( window.location.href === "https://omnifora.com/c/redirect-politifora" ) {
		window.location.replace( "https://politifora.com/" );
	}
});

```

As should be obvious from the code, [Omnifora](https://omnifora.com/) is the primary site in this network and I am attempting to redirect a [Politifora category on Omnifora](https://omnifora.com/c/redirect-politifora) to [Politifora](https://politifora.com/). I’ve already eliminated caching (browser, server, Cloudflare) as a potential culprit. Is there some quirk in Discourse that would cause this redirect not to trigger?

---

<div class="post-metadata">

### Author: ![Cameron\_D](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/cameron_d/32/97535_2.png) [@Cameron\_D](https://meta.discourse.org/u/Cameron_D)
#### Post date: [4월 20, 2018, 4:45오전 UTC](https://meta.discourse.org/t/redirect-category-to-external-url/85691/7 "2018-04-20T04:45:06Z")

</div>

> [@Zyniker](#):
>
> Is there some quirk in Discourse that would cause this redirect not to trigger?

Yes, being a single-page-app the page only loads once, subsequent loads just replace the page contents. There is a page change API you can hook into:

> [@How do you force a script to refire on every page load in Discourse?](https://meta.discourse.org/t/how-do-you-force-a-script-to-refire-on-every-page-load-in-discourse/74856/5):
>
> I think you can use the PluginAPI onPageChange function to do what you are looking for. See: [Using the PluginAPI in Site Customizations](https://meta.discourse.org/t/using-the-pluginapi-in-site-customizations/41281) Here’s an example function: \<script type="text/discourse-plugin" version="0.8"\> api.onPageChange(() =\>{ randBackground(); }); function randBackground() { $('body').css('background-color', '#'+(Math.random()\*0xFFFFFF\<\<0).toString(16)); } \</script\> Update: If you are looking to apply similar changes to post bodies, see [post…](https://meta.discourse.org/t/how-do-you-force-a-script-to-refire-on-every-page-load-in-discourse/74856/26)

---

<div class="post-metadata">

### Author: ![Zyniker](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zyniker/32/120004_2.png) [@Zyniker](https://meta.discourse.org/u/Zyniker)
#### Post date: [4월 20, 2018, 5:32오전 UTC](https://meta.discourse.org/t/redirect-category-to-external-url/85691/8 "2018-04-20T05:32:42Z")

</div>

I’m sure this is the right way to accomplish the redirect, but I am not sure _how_ to write the necessary code. I’ve tried the following and neither seems to work:

```plaintext
api.onPageChange((url) => {
	var targeturl = 'https://politifora.com/';
	if ( url === '/c/redirect-politifora' ) {
		$(location).attr('href',targeturl);
	}
});

```

and

```plaintext
api.onPageChange((url) => {
	if ( url === '/c/redirect-politifora' ) {
		window.location.replace( 'https://politifora.com/' );
	}
});

```

---

<div class="post-metadata">

### Author: ![Flower\_Child](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/flower_child/32/94987_2.png) [@Flower\_Child](https://meta.discourse.org/u/Flower_Child)
#### Post date: [4월 20, 2018, 6:21오전 UTC](https://meta.discourse.org/t/redirect-category-to-external-url/85691/9 "2018-04-20T06:21:16Z")

</div>

if you wanted a redirect this is the way to do it

```plaintext
api.onPageChange((url) => {
	if (url.includes('/c/redirect-politifora')) {
		window.location.replace('https://politifora.com/');
	}
});

```

however this is kinda slow and won’t run until the page has finished loading, which means users will still see the redirect category. i tried a second way, which is using jQuery to replace the _links_ on the index or anywhere else this category appears.

```plaintext
api.onPageChange(() => {
  $(function(){
    $('a[href*="/c/redirect-politifora').attr(
      'href', 'https://politifora.com/'
    ); 
  });
});

```

with this you don’t need a redirect, because it modifies href attribute of every redirect-politiflora link in place.

---

<div class="post-metadata">

### Author: ![Zyniker](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zyniker/32/120004_2.png) [@Zyniker](https://meta.discourse.org/u/Zyniker)
#### Post date: [4월 20, 2018, 6:42오전 UTC](https://meta.discourse.org/t/redirect-category-to-external-url/85691/10 "2018-04-20T06:42:40Z")

</div>

Perhaps I’m missing something, but I tried each of those solutions (both imported from an external .js and dropped in as a customization) and was unable to get either one to work.

---

<div class="post-metadata">

### Author: ![Flower\_Child](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/flower_child/32/94987_2.png) [@Flower\_Child](https://meta.discourse.org/u/Flower_Child)
#### Post date: [4월 20, 2018, 7:14오전 UTC](https://meta.discourse.org/t/redirect-category-to-external-url/85691/11 "2018-04-20T07:14:42Z")

</div>

where are you inserting them, and do you have the correct script tags around them

---

<div class="post-metadata">

### Author: ![Zyniker](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zyniker/32/120004_2.png) [@Zyniker](https://meta.discourse.org/u/Zyniker)
#### Post date: [4월 20, 2018, 7:27오전 UTC](https://meta.discourse.org/t/redirect-category-to-external-url/85691/12 "2018-04-20T07:27:13Z")

</div>

I tried both the header (using `defer`) and the footer (via Customize). When inserted via Customize, I had the code surrounded by `<script></script>` with `type="text/discourse-plugin"` set.

### Update:

The `window.location.replace` option is currently live on [Omnifora](https://omnifora.com/). I am using the following code (yes, I have tried a number of different matching options, but none of them seem to work):

**Code Dropped into Customize**

```plaintext
<script type="text/discourse-plugin" version="0.8.19">
api.onPageChange((url) => {
    if ( url === 'c/redirect/politifora' || url === '/c/redirect-politifora' || url === 'https://omnifora.com/c/redirect-politifora' || url.includes('/c/redirect-politifora') || url.match('/c/redirect/politifora') ) {
        window.location.replace('https://politifora.com/');
    }
});
</script>

```

**Code as Rendered on Page**

```plaintext
<script>Discourse._registerPluginCode('0.8.19', function (api) {

    api.onPageChange(function (url) {
        if (url === 'c/redirect/politifora' || url === '/c/redirect-politifora' || url === 'https://omnifora.com/c/redirect-politifora' || url.includes('/c/redirect-politifora') || url.match('/c/redirect/politifora')) {
            window.location.replace('https://politifora.com/');
        }
    });
});</script>

```

Given the wrapper that Discourse appears to have applied, it seems the code is _partially_ working, but not performing its intended purpose (i.e., redirecting that category to [Politifora](https://politifora.com)). The live code (and its failure to function) can both be viewed on the aforementioned site (the target/affected category is currently at the bottom of the category list).

---

<div class="post-metadata">

### Author: ![Zyniker](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zyniker/32/120004_2.png) [@Zyniker](https://meta.discourse.org/u/Zyniker)
#### Post date: [4월 26, 2018, 1:48오전 UTC](https://meta.discourse.org/t/redirect-category-to-external-url/85691/13 "2018-04-26T01:48:29Z")

</div>

Is there some specific version of the API I should be using for this?

---

<div class="post-metadata">

### Author: ![Zyniker](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zyniker/32/120004_2.png) [@Zyniker](https://meta.discourse.org/u/Zyniker)
#### Post date: [5월 8, 2018, 6:41오전 UTC](https://meta.discourse.org/t/redirect-category-to-external-url/85691/14 "2018-05-08T06:41:24Z")

</div>

The following script achieves the intended result when added to `</head>` via Customize:

```plaintext
<script type="text/discourse-plugin" version="0.8.19">
api.onPageChange(() => {
	if ( window.location.href === "https://omnifora.com/c/redirect-politifora" ) {
		window.location.replace( "https://politifora.com/" );
	}
});
</script>

```

---

<div class="post-metadata">

### Author: ![cogdog](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/cogdog/32/116536_2.png) [@cogdog](https://meta.discourse.org/u/cogdog)
#### Post date: [8월 6, 2021, 7:58오후 UTC](https://meta.discourse.org/t/redirect-category-to-external-url/85691/15 "2021-08-06T19:58:22Z")

</div>

감사합니다! 하위 카테고리 중 하나가 리다이렉트로 작동하도록 설정해야 할 필요가 있었어요. 제 커스터마이즈 코드에 이미 `onPageChange()` 체크가 포함되어 있거든요(홈페이지에서만 헤더를 표시하는 용도로 사용 중). 그래서 이 방식도 잘 작동할 거고, 사이트의 전체 URL에 하드코딩되어 있지 않아요:

```plaintext
<script type="text/discourse-plugin" version="0.8.19">
api.onPageChange((url) => {
       // 홈페이지에 있는지 확인 
        if (url === "/" || url === homeRoute) {
              // 홈페이지용 커스텀 작업 수행
       } else if ( url=== "/c/redirect-politifora" ) {
             // 카테고리 리다이렉트
             window.location.replace( "https://politifora.com/" );
       } else {
             // 필요한 경우 모든 다른 페이지에서 작업 수행
       }
});
</script>

```

---

<div class="post-metadata">

### Author: ![cogdog](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/cogdog/32/116536_2.png) [@cogdog](https://meta.discourse.org/u/cogdog)
#### Post date: [9월 21, 2021, 7:13오후 UTC](https://meta.discourse.org/t/redirect-category-to-external-url/85691/16 "2021-09-21T19:13:48Z")

</div>

사실 저는 사용하지 않았습니다. 하위 카테고리 브라우저가 다른 곳으로 리다이렉트되면서 너무 혼란스러운 지연이 발생했거든요. 하지만 어쨌든 새로운 것을 배웠습니다.

---

<div class="post-metadata">

### Author: ![JonathanShaw](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jonathanshaw/32/532672_2.png) [@JonathanShaw](https://meta.discourse.org/u/JonathanShaw)
#### Post date: [9월 21, 2021, 10:32오후 UTC](https://meta.discourse.org/t/redirect-category-to-external-url/85691/17 "2021-09-21T22:32:43Z")

</div>

> [@cogdog](#):
>
> 서브 카테고리 브라우저가 다른 곳으로 리디렉션되는 동안 너무 많은 혼란스러운 지연이 발생했습니다.

페이지가 렌더링된 후에 발화되는 것 같은 `api.onPageChange()`에 의존하기보다 라우트를 수정하면 이 문제가 덜 발생할 수 있습니다.

[Redirect an existing route in a theme component](https://meta.discourse.org/t/how-to-redirect-an-existing-disourse-route-in-a-theme-component/201900) 를 참조하세요.

---

<div class="post-metadata">

### Author: ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)
#### Post date: [6월 8, 2024, 12:36오후 UTC](https://meta.discourse.org/t/redirect-category-to-external-url/85691/18 "2024-06-08T12:36:52Z")

</div>

이 주제는 2242일 후 자동으로 닫혔습니다. 더 이상 답변을 작성할 수 없습니다.
