# Discourse rewriting url path behaviour failing due to subfolder

**URL:** <https://meta.discourse.org/t/discourse-rewriting-url-path-behaviour-failing-due-to-subfolder/94120>\
**Category:** Bug\
**Tags:** subfolder\
**Created:** [August 7, 2018, 1:40pm UTC](https://meta.discourse.org/t/discourse-rewriting-url-path-behaviour-failing-due-to-subfolder/94120 "2018-08-07T13:40:04Z")\
**Posts on this page:** 11\
**Page:** 1

## AI-generated summary

**Discourse rewriting url path behaviour failing due to subfolder**

[Bram_Rongen](/t/-/94120/1) reported an issue with Discourse rewriting URL paths, resulting in a redirect to a non-existent page. The problem occurs when visiting a link like `https://www.3dhubs.com/talk/t/talk-forums-disappearing/10681`, which gets redirected to `https://www.3dhubs.com/talk/t-forums-disappearing/10681`. 

[Bram_Rongen](/t/-/94120/1) suspects that Discourse is being misled by the `talk/t/talk` part of the URL. [RGJ](/t/-/94120/3) used safe mode to investigate and found that the correct content is returned, but then a 404 body is fetched.

[Bram_Rongen](/t/-/94120/4) created a PR to fix the issue, but [sam](/t/-/94120/5) considers the fix not ideal and wonders if something is not configured correctly on [Bram_Rongen](/t/-/94120/1)'s end.

[RGJ](/t/-/94120/7) and [Bram_Rongen](/t/-/94120/10) further investigated and found that the issue only occurs when coming from a direct URL, not when browsing to the topic.

[neil](/t/-/94120/12) eventually pushed a fix that looks only at the beginning of the path, avoiding the problem unless the BaseUri starts with `/t`, `/u`, or similar, which is not recommended.

<div class="post-metadata">

**Author:** ![Bram\_Rongen](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/bram_rongen/32/106220_2.png) [@Bram\_Rongen](https://meta.discourse.org/u/Bram_Rongen)\
**Post date:** [August 7, 2018, 1:40pm UTC](https://meta.discourse.org/t/discourse-rewriting-url-path-behaviour-failing-due-to-subfolder/94120/1 "2018-08-07T13:40:04Z")

</div>

Hi there!

We just migrated our forum to Discourse and I’m running into the following issue. When visiting: [Talk Forums Disappearing? - No category - Talk Manufacturing | Hubs](https://www.3dhubs.com/talk/t/talk-forums-disappearing/10681) the user is being redirectd to `https://www.3dhubs.com/talk/t-forums-disappearing/10681` which doesn’t exist.

You can see the behaviour in practice by clicking the link. The post does exist, when you for instance search for it and navigate through it that way it works fine.

My guess is Discourse is trying to be clever about the urls and is misguided by the `talk/t/talk` part.

What’s the recommended way to solve this?

Cheers, Bram

---

<div class="post-metadata">

**Author:** ![Bram\_Rongen](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/bram_rongen/32/106220_2.png) [@Bram\_Rongen](https://meta.discourse.org/u/Bram_Rongen)\
**Post date:** [August 7, 2018, 7:26pm UTC](https://meta.discourse.org/t/discourse-rewriting-url-path-behaviour-failing-due-to-subfolder/94120/2 "2018-08-07T19:26:42Z")

</div>

Some help from my hosting:

> Weird bug indeed. I don’t see where it redirects either, seems to be returning the correct page and then redirected by Javascript. It’s not the nginx config, it is reaching the application server.

---

<div class="post-metadata">

**Author:** ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)\
**Post date:** [August 7, 2018, 8:24pm UTC](https://meta.discourse.org/t/discourse-rewriting-url-path-behaviour-failing-due-to-subfolder/94120/3 "2018-08-07T20:24:07Z")

</div>

Using [safe mode](https://meta.discourse.org/t/53504?silent=true) to make sure there is no plugin or theme causing this

1 - correct link is requested  
2 - correct content is returned  
but then:  
3 - fetching 404 body

 ![image](https://global.discourse-cdn.com/meta/original/3X/2/3/237b2649c8bba455ced3ac08ec767e391de6290c.png)

logging

```
[db5060] Started GET "/talk/t/talk-forums-disappearing/10681?safe_mode=no_custom%2Cno_plugins%2Conly_official" for 77.163.254.28 at 2018-08-07 20:19:10 +0000
[db5060] Processing by TopicsController#show as HTML
[db5060] Parameters: {"safe_mode"=>"no_custom,no_plugins,only_official", "slug"=>"talk-forums-disappearing", "topic_id"=>"10681"}
[db5060] Rendering topics/show.html.erb within layouts/application
[db5060] Rendered topics/show.html.erb within layouts/application (6.3ms)
[db5060] Rendered layouts/_head.html.erb (0.3ms)
[db5060] Rendered common/_discourse_stylesheet.html.erb (0.1ms)
[db5060] Rendered common/_special_font_face.html.erb (0.1ms)
[db5060] Rendered application/_header.html.erb (0.2ms)
[db5060] Rendered common/_discourse_javascript.html.erb (0.3ms)
[db5060] Completed 200 OK in 124ms (Views: 9.0ms | ActiveRecord: 26.0ms)

[db5060] Started GET "/talk/404-body?_=1533673157932" for 77.163.254.28 at 2018-08-07 20:19:12 +0000
[db5060] Processing by ExceptionsController#not_found_body as HTML
[db5060] Parameters: {"_"=>"1533673157932"}
[db5060] Rendering exceptions/not_found.html.erb
[db5060] Rendered exceptions/not_found.html.erb (15.3ms)
[db5060] Rendering html template
[db5060] Rendered html template (0.0ms)
[db5060] Completed 200 OK in 52ms (Views: 0.3ms | ActiveRecord: 11.6ms)

```

---

<div class="post-metadata">

**Author:** ![Bram\_Rongen](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/bram_rongen/32/106220_2.png) [@Bram\_Rongen](https://meta.discourse.org/u/Bram_Rongen)\
**Post date:** [August 7, 2018, 8:30pm UTC](https://meta.discourse.org/t/discourse-rewriting-url-path-behaviour-failing-due-to-subfolder/94120/4 "2018-08-07T20:30:52Z")

</div>

Created a PR to fix it: [FIX: wrongful redirections when topic starts with BaseUri by bram-rongen · Pull Request #6243 · discourse/discourse · GitHub](https://github.com/discourse/discourse/pull/6243)

I don’t have any prior experience in _Ember_ or _Rails_ or _Discourse_ so please review carefully 🙂 and then 🚢 it!

---

<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:** [August 8, 2018, 5:15am UTC](https://meta.discourse.org/t/discourse-rewriting-url-path-behaviour-failing-due-to-subfolder/94120/5 "2018-08-08T05:15:36Z")

</div>

That fix is not ideal… @neil can you have a look at this issue

It does not make sense to me cause this topic works just fine:

> **[Community e-book?](https://www.sitepoint.com/community/t/community-e-book/96166)**
>
> Hi, I seem to remember some time ago that a community e-book was available on Sitepoint? It had bits of information from lots of respectable members in lots of different fields related to web development. I thought it looked like a really good...

I wonder if something is not configured right on your end Bram.

---

<div class="post-metadata">

**Author:** ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)\
**Post date:** [August 8, 2018, 6:48am UTC](https://meta.discourse.org/t/discourse-rewriting-url-path-behaviour-failing-due-to-subfolder/94120/7 "2018-08-08T06:48:56Z")

</div>

> [@sam](#):
>
> It does not make sense to me cause this topic works just fine:

but… but… it doesn’t:

 ![image](https://global.discourse-cdn.com/meta/original/3X/1/9/196675d3f0861bc5896431e7eb523ad1263e41d4.png)

---

<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:** [August 8, 2018, 6:50am UTC](https://meta.discourse.org/t/discourse-rewriting-url-path-behaviour-failing-due-to-subfolder/94120/8 "2018-08-08T06:50:59Z")

</div>

well it was working earlier for some crazy reason, @neil will look at this

---

<div class="post-metadata">

**Author:** ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)\
**Post date:** [August 8, 2018, 7:06am UTC](https://meta.discourse.org/t/discourse-rewriting-url-path-behaviour-failing-due-to-subfolder/94120/10 "2018-08-08T07:06:28Z")

</div>

It works fine when you browse to such a topic, it only fails when you come from a direct URL.

---

<div class="post-metadata">

**Author:** ![Bram\_Rongen](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/bram_rongen/32/106220_2.png) [@Bram\_Rongen](https://meta.discourse.org/u/Bram_Rongen)\
**Post date:** [August 8, 2018, 8:14am UTC](https://meta.discourse.org/t/discourse-rewriting-url-path-behaviour-failing-due-to-subfolder/94120/11 "2018-08-08T08:14:24Z")

</div>

> [@sam](#):
>
> That fix is not ideal… @neil can you have a look at this issue

I agree, it might be better to restructure the code to not require this functionality at all but it does make the function do what it is supposed to do:

```plaintext
/**
    Returns the current `location.pathname` without rootURL
*/
```

Instead of just replacing every occurrence of `Discourse.BaseUri` with an empty string my version only drops the baseUri when it exists on the start of the url. The only case that this will break is when you decide to use any of the starts of system paths (/u, /t, /admin, etc) as your BaseUri. This will break your site, but not more then before.

---

<div class="post-metadata">

**Author:** ![neil](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/neil/32/102150_2.png) [@neil](https://meta.discourse.org/u/neil)\
**Post date:** [August 8, 2018, 6:04pm UTC](https://meta.discourse.org/t/discourse-rewriting-url-path-behaviour-failing-due-to-subfolder/94120/12 "2018-08-08T18:04:45Z")

</div>

I pushed a fix that looks only at the beginning of the path which avoids this problem. Unless you use `/t`, `/u`, or something like that, but that would be a terrible idea for many reasons.

---

<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:** [August 12, 2018, 1:00am UTC](https://meta.discourse.org/t/discourse-rewriting-url-path-behaviour-failing-due-to-subfolder/94120/13 "2018-08-12T01:00:18Z")

</div>

Closed on August 12, 2018, 1:00am UTC

This topic was automatically closed after 3 days. New replies are no longer allowed.
