Simple Regex redirect question

When my site was hosted elsewhere some URLS were in this date + postname format:

https://billbennett.co.nz/2016/06/03/nz-wireless-rural-broadband-key-milestone/

I used regex to redirect them to a simpler format.

https://billbennett.co.nz/nz-wireless-rural-broadband-key-milestone/

This is the Regex:

/(\d*)/(\d*)/(\d*)/([A-Za-z0-9-]*)/gm

I tested it on Regex101 and it seems to work as expected

However, when I enter this into redirects.json as:

{
“from”: “/(\d*)/(\d*)/(\d*)/([A-Za-z0-9-]*)/gm”,
“to”: “/$4/”,
“permanent”: true
}

I get this error:

“Could not parse JSON: Unexpected token d in JSON at position 10366”

What am I doing wrong?

I suspect someone who is more familiar with using Regex for Ghost redirects will find this easy to correct. Can you help?

I can’t guarantee your regex will work, but the error is coming because \ is an escape character in json - try /(\\d*)/(\\d*)/(\\d*)/([A-Za-z0-9-]*)/ (I intentionally removed the gm flags - I don’t think that would work)

2 Likes

Thanks Vikas

That seems to work.

1 Like