grav icon indicating copy to clipboard operation
grav copied to clipboard

Redirect regex pattern to match entire $source_url

Open Ralla opened this issue 6 years ago • 3 comments

Problem The regex checking for redirects defined in site.yaml, allows and passes on just a part of the path.

Example site.yaml

redirects:
  /this-is-my-url: '/somepath'
  /this-is-sparta: '/some-other-path'
  /this: '/some-different-path'

With this configuration, when hitting /this, I would get redirected to /somepath because it matches on the first redirects entry.

This happens because the current regex only checks start of string until found:

#^\/this#

Adding an end of string check in the regex, will not match on the first two redirects, but continue on until it matches on the last entry:

#^\/this$#

Ralla avatar Sep 12 '18 09:09 Ralla

What about /this/is/my/new/path, it should redirect to /some-different-path/is/my/new/path

mahagr avatar Sep 13 '18 14:09 mahagr

Could we have this as a config toggle though? Developers inherit sites to migrate to Grav that have all kinds of crazy URL systems. I am doing one now where having such a toggle would allow me to put in a full set of redirects. Without this, I can't see a way to make them all work :/

hughbris avatar Jan 10 '22 23:01 hughbris

Instead of implementing the patch and breaking all redirects, you can do this instead:

redirects:
  '/this-is-my-url$': '/somepath'
  '/this-is-sparta$': '/some-other-path'
  '/this$': '/some-different-path'

mahagr avatar Jan 11 '22 09:01 mahagr