# Help! I need to redirect /p/?

**URL:** <https://forum.ghost.org/t/help-i-need-to-redirect-p/40321>\
**Category:** Routing\
**Created:** [August 7, 2023, 12:20pm UTC](https://forum.ghost.org/t/help-i-need-to-redirect-p/40321 "2023-08-07T12:20:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cathy\_Sarisky](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/cathy_sarisky/32/25750_2.png) [@Cathy\_Sarisky](https://forum.ghost.org/u/Cathy_Sarisky)\
**Post date:** [August 7, 2023, 12:20pm UTC](https://forum.ghost.org/t/help-i-need-to-redirect-p/40321/1 "2023-08-07T12:20:46Z")

</div>

A client’s substack migration had posts with /p/post-name. I wrote a regex redirect rule to /postname/ and thought the problem was fixed.

And then, today he tried to preview a post, and got a 404. Apparently the post preview names those unpublished posts /p/uuid , which then redirects them, which breaks the preview.

So… I’ll file a Github issue, because the preview probably should ignore the redirects file. But meanwhile, if anyone has a suggestion that doesn’t involve writing 100 individual redirects into redirects.yaml, I’d be most appreciative. 🙂

(This post makes finding the problem sound a LOT simpler than it actually was. That’s an hour of my life I’m not getting back!!)

---

<div class="post-metadata">

**Author:** ![mjw](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/mjw/32/30781_2.png) [@mjw](https://forum.ghost.org/u/mjw)\
**Post date:** [August 7, 2023, 12:37pm UTC](https://forum.ghost.org/t/help-i-need-to-redirect-p/40321/2 "2023-08-07T12:37:18Z")

</div>

@Cathy_Sarisky, have you considered using a rewrite in Nginx?

```auto
location /p {
 rewrite ^/p(/.*)$ $1 last;
}

```

---

<div class="post-metadata">

**Author:** ![Hannah](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/hannah/32/22_2.png) [@Hannah](https://forum.ghost.org/u/Hannah)\
**Post date:** [August 7, 2023, 12:52pm UTC](https://forum.ghost.org/t/help-i-need-to-redirect-p/40321/3 "2023-08-07T12:52:21Z")

</div>

Previews are built deeply into the frontend with a hardcoded url of `/p/:uuid/` as you discovered. You’re right they probably should ignore redirects, but changing that would be significant and is unlikely to happen outside of a huge rewrite of the frontend.

The best solution I can think of is to use a regex match for `/p/` followed by not-a-uuid as they have a very specific structure.

I asked ChatGPT to write this regex, and it gave me this:

```auto
const regex = /^\/p\/(?![0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$)/;

const url1 = '/p/abc123';
const url2 = '/p/12345678-1234-5678-1234-567812345678';

console.log(regex.test(url1)); // Output: true
console.log(regex.test(url2)); // Output: false

```

Looks like it would work!

---

<div class="post-metadata">

**Author:** ![Cathy\_Sarisky](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/cathy_sarisky/32/25750_2.png) [@Cathy\_Sarisky](https://forum.ghost.org/u/Cathy_Sarisky)\
**Post date:** [August 7, 2023, 12:59pm UTC](https://forum.ghost.org/t/help-i-need-to-redirect-p/40321/4 "2023-08-07T12:59:10Z")

</div>

Ghost pro, so nope. :)

---

<div class="post-metadata">

**Author:** ![Cathy\_Sarisky](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/cathy_sarisky/32/25750_2.png) [@Cathy\_Sarisky](https://forum.ghost.org/u/Cathy_Sarisky)\
**Post date:** [August 7, 2023, 3:01pm UTC](https://forum.ghost.org/t/help-i-need-to-redirect-p/40321/5 "2023-08-07T15:01:09Z")

</div>

Great idea, Hannah, thank you! :)

Fortunately, the migrated content doesn’t have uuid slugs!

---

<div class="post-metadata">

**Author:** ![Cathy\_Sarisky](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/cathy_sarisky/32/25750_2.png) [@Cathy\_Sarisky](https://forum.ghost.org/u/Cathy_Sarisky)\
**Post date:** [August 7, 2023, 4:56pm UTC](https://forum.ghost.org/t/help-i-need-to-redirect-p/40321/6 "2023-08-07T16:56:27Z")

</div>

> [@Hannah](#):
>
> `^\/p\/(?![0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$)`

For anyone who needs it in the future, this is what worked. (I needed to capture, not just match)

```auto
  ^\/p\/((?![0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}\/$)[A-z0-9-_.]+)\/$: /$1

```
