# 404 rendering at custom route without redirection

**URL:** https://meta.discourse.org/t/404-rendering-at-custom-route-without-redirection/89349
**Category:** Development
**Created:** [June 7, 2018, 9:20am UTC](https://meta.discourse.org/t/404-rendering-at-custom-route-without-redirection/89349 "2018-06-07T09:20:27Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![arjen](https://avatars.discourse-cdn.com/v4/letter/a/b9bd4f/32.png) [@arjen](https://meta.discourse.org/u/arjen)
#### Post date: [June 7, 2018, 9:20am UTC](https://meta.discourse.org/t/404-rendering-at-custom-route-without-redirection/89349/1 "2018-06-07T09:20:27Z")

</div>

Dear Discourse community,

I have an issue I’m trying to resolve, but having little problems with it.  
The behaviour I’m trying to achieve with a custom route, is basically the behaviour you see when linking in a topic to another non-existent topic. When clicking a link to a non-existent topic, you are presented with a 404 page. When you click your browser’s Back button, you are redirected back to the topic you came from.

Now for my custom route, I notice different behaviour. So when I’m at my custom route, a JSON is retrieved from the backend, but when this JSON is not present, the controller returns a 404 page. EmberJS picks up on this, and redirects to [www.my-discourse-server.com/404](http://www.my-discourse-server.com/404). So far so good. But if I hit the back button, I go back to the non-existent page, which returns a 404 and I once again find myself at the /404 page.

Let me try to give a short indication of what the code looks like. My route:

```javascript
export default Discourse.Route.extend({
  model(opts, router, model) {
    return Ember.$.getJSON(`/custom-route/${opts.uniqueID}`);
  }
})

```

When this returns a 404, it is caught by Discourse and transitioned to ‘`exception-unknown`’ by `/discourse/app/assets/javascripts/discourse/routes/application.js.es6`.

So basically what I want is to present the user with a 404 page, without the URL redirecting to /404.

Any suggestions?

Thanks!

---

<div class="post-metadata">

### Author: ![Bas](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/bas/32/294929_2.png) [@Bas](https://meta.discourse.org/u/Bas)
#### Post date: [June 11, 2018, 7:35am UTC](https://meta.discourse.org/t/404-rendering-at-custom-route-without-redirection/89349/2 "2018-06-11T07:35:55Z")

</div>

@joebuhlig is this something that you encountered when developing the Static Pages plugin?

---

<div class="post-metadata">

### Author: ![joebuhlig](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/joebuhlig/32/193054_2.png) [@joebuhlig](https://meta.discourse.org/u/joebuhlig)
#### Post date: [June 11, 2018, 12:01pm UTC](https://meta.discourse.org/t/404-rendering-at-custom-route-without-redirection/89349/3 "2018-06-11T12:01:30Z")

</div>

Not exactly. The [Static Pages plugin](https://meta.discourse.org/t/static-pages-plugin-dl-static-pages/69698) doesn’t try to maintain the URL without redirecting. It’s all done on the server side as seen here:

[https://github.com/procourse/procourse-static-pages/blob/master/app/controllers/dl\_static\_pages/pages\_controller.rb](https://github.com/procourse/procourse-static-pages/blob/master/app/controllers/dl_static_pages/pages_controller.rb)

---

<div class="post-metadata">

### Author: ![arjen](https://avatars.discourse-cdn.com/v4/letter/a/b9bd4f/32.png) [@arjen](https://meta.discourse.org/u/arjen)
#### Post date: [June 14, 2018, 7:33am UTC](https://meta.discourse.org/t/404-rendering-at-custom-route-without-redirection/89349/4 "2018-06-14T07:33:50Z")

</div>

Hey @joebuhlig,

I actually created PRs for the procourse-static-pages plugin recently. Glad you removed the Licensing, since I think more people can benefit from this plugin.

When I make a post, linking to a non-existing procourse-static-page, I see the same behaviour as I described. So when you click the link in the post, Discourse opens that URL and receives a 404 from the controller (as intended). However, the frontend then redirects you to /404. Meaning that if you go back, you again hit the non-existing route which again redirects you to the /404.

We’re still dealing with this problem, and I think your plugin can also benefit from a solution.

Arjen

---

<div class="post-metadata">

### Author: ![joebuhlig](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/joebuhlig/32/193054_2.png) [@joebuhlig](https://meta.discourse.org/u/joebuhlig)
#### Post date: [June 14, 2018, 10:54am UTC](https://meta.discourse.org/t/404-rendering-at-custom-route-without-redirection/89349/5 "2018-06-14T10:54:18Z")

</div>

The Static Pages plugin simply follows the core Discourse process. I try to stick to the “the Discourse way” as much as I can.

Since this seems to be the way it works in core, you would need to write an acceptable PR for the team. But I can’t speak to whether or not they would accept it. There may be a valid reason for redirecting to a 404 page as opposed to leaving the URL.

---

<div class="post-metadata">

### Author: ![arjen](https://avatars.discourse-cdn.com/v4/letter/a/b9bd4f/32.png) [@arjen](https://meta.discourse.org/u/arjen)
#### Post date: [June 14, 2018, 11:21am UTC](https://meta.discourse.org/t/404-rendering-at-custom-route-without-redirection/89349/6 "2018-06-14T11:21:35Z")

</div>

I agree that it seems to be “the Discourse way”. However, when I link to a non-existing topic/post, Discourse does not redirect to a 404 page, but simply renders one without redirecting. So there seem to be multiple ways of handling this.

Any chance @codinghorror can weigh in on this?

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [December 22, 2020, 2:56pm UTC](https://meta.discourse.org/t/404-rendering-at-custom-route-without-redirection/89349/7 "2020-12-22T14:56:03Z")

</div>


