# Client-side path rewrite failing with subfolder setup

**URL:** https://meta.discourse.org/t/client-side-path-rewrite-failing-with-subfolder-setup/179504
**Category:** Bug
**Tags:** subfolder
**Created:** [February 12, 2021, 7:39pm UTC](https://meta.discourse.org/t/client-side-path-rewrite-failing-with-subfolder-setup/179504 "2021-02-12T19:39:40Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![renato](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/renato/32/383632_2.png) [@renato](https://meta.discourse.org/u/renato)
#### Post date: [February 12, 2021, 7:39pm UTC](https://meta.discourse.org/t/client-side-path-rewrite-failing-with-subfolder-setup/179504/1 "2021-02-12T19:39:40Z")

</div>

Continuing the discussion from [Discourse rewriting url path behaviour failing due to subfolder](https://meta.discourse.org/t/discourse-rewriting-url-path-behaviour-failing-due-to-subfolder/94120/12):

I have the exact same issue from the aforementioned topic, in which the URL is rewritten only in scenarios where the start of a sub-route is equal to the subfolder. I’m using `/f` as subfolder, I understand the caveats and pains of this setup, but everything else is working fine so I’d like to get some help fixing this, if possible.

> [@Discourse rewriting url path behaviour failing due to subfolder](https://meta.discourse.org/t/discourse-rewriting-url-path-behaviour-failing-due-to-subfolder/94120/12):
>
> 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.

I’m not using an existing Discourse route, but if the one-letter subfolder is an issue I’d like to try fixing it before considering a different setup.

Some routes that get rewritten:

- `/f/t/food-chain-magnate/4826` → `/f/tood-chain-magnate/4826`
- `/f/tag/food-chain-magnate` → `/f/tagood-chain-magnate`
- `/f/u/renato/follow/following` → `/f/u/renatoollow/following`
- `/f/u/fred/summary` → `/f/ured/summary`

As it’s a client-side rewrite, CURL’ing the same URLs works fine.

[Here’s the commit](https://github.com/discourse/discourse/commit/35bef72d4ed6d530468bdc091bc076d431a2cdc4) that originally fixed this, but [getURL](https://github.com/discourse/discourse/blob/master/app/assets/javascripts/discourse/app/lib/discourse-location.js#L65-L71) has changed to use `get-url` helpers instead of `Discourse.BaseUri`.

Following the calls to that `getURL`, `location.pathname` is correct on first call (starting with `/f`), but on one of the next calls the subfolder gets stripped and becomes `/t/f-started-slug/id`, causing [this replace](https://github.com/discourse/discourse/blob/ae6449016281f2055fbd03d13d7bfa5b3d93539d/app/assets/javascripts/discourse-common/addon/lib/get-url.js#L51) to act on that `/f`.

I don’t know enough of Discourse internals to fully understand where this rewrite is taking place, but testing in my instance, enforcing the replace in `withoutPrefix` to only act at the beginning of `path` seems to fix it.

```js
// changing ...
return path.replace(rootURL, "");
// to something like ... (assuming rootURL doesn't need to be escaped)
return path.replace(new RegExp("^" + rootURL), "")
// or without regex ...
return path.indexOf(rootURL) === 0 ? path.slice(rootURL.length) : path;

```

I don’t know if that would be a possible fix or if it would introduce any regression, any help is appreciated.

---

<div class="post-metadata">

### Author: ![renato](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/renato/32/383632_2.png) [@renato](https://meta.discourse.org/u/renato)
#### Post date: [February 12, 2021, 8:15pm UTC](https://meta.discourse.org/t/client-side-path-rewrite-failing-with-subfolder-setup/179504/2 "2021-02-12T20:15:01Z")

</div>

Oh well, a better search on existing topics would lead me to a reply from yesterday that might fix this…  
[https://meta.discourse.org/t/two-bugs-with-usernames-starting-with-subfolder-name/169505/6](https://meta.discourse.org/t/two-bugs-with-usernames-starting-with-subfolder-name/169505/6)

**EDIT** : I just upgraded to _70050a8ba3_ and the issue persists.

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [February 15, 2021, 4:10pm UTC](https://meta.discourse.org/t/client-side-path-rewrite-failing-with-subfolder-setup/179504/4 "2021-02-15T16:10:53Z")

</div>

I will take a look a this as time permits.

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [February 18, 2021, 10:21pm UTC](https://meta.discourse.org/t/client-side-path-rewrite-failing-with-subfolder-setup/179504/5 "2021-02-18T22:21:48Z")

</div>

> [@renato](#):
>
> enforcing the replace in `withoutPrefix` to only act at the beginning of `path` seems to fix it.

This looks to be the way indeed, thanks for the investigation and bug report. This PR should fix it:

[https://github.com/discourse/discourse/pull/12135](https://github.com/discourse/discourse/pull/12135)

Can you give it a try @renato ?

---

<div class="post-metadata">

### Author: ![renato](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/renato/32/383632_2.png) [@renato](https://meta.discourse.org/u/renato)
#### Post date: [February 22, 2021, 1:26pm UTC](https://meta.discourse.org/t/client-side-path-rewrite-failing-with-subfolder-setup/179504/6 "2021-02-22T13:26:28Z")

</div>

It’s working great now, thanks!

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [February 27, 2021, 11:00am UTC](https://meta.discourse.org/t/client-side-path-rewrite-failing-with-subfolder-setup/179504/7 "2021-02-27T11:00:47Z")

</div>

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