Simple Regex redirect question

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

I used regex to redirect them to a simpler format.

This is the Regex:


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)


Thanks Vikas

That seems to work.

1 Like