Problem/Motivation
It'd be nice if users chould have a redirect regex such as: ^/foo/(\d+)$ and have the redirect reference replace it with /baz/$1/target
Such that /foo/122 redirects to /baz/122/target.
Proposed resolution
Add appropriate preg_replace() call to ensure it updates regex references.
Add some kind of flag into the regex: prefix, signifying that the regex needs to match against the query strings too. e.g. :regex:qs:
So that regex such as this is possible:
regex:qs:page(?:\?)(?:.*?(\&))?(param)=([^&]+)
to match against page?extraparam=foo¶m=123
Remaining tasks
Provide MR.
Comments
Comment #3
codebymikey commentedComment #4
codebymikey commentedOne limitation at the moment with the module is with regards to how the
?character is handled.I would ideally like to be able to use named regexes capturing groups like:
^/foo/(?'id'\d+)->/baz/$idBut due to how the RedirectForm currently works, the presence of the
?will cause it to think it's a query string and mess the original URL up.Simple non-named regex groups will still work for now, just that more complex regexes will no longer be supported.
I think to combat this, the "default" and "edit" form handlers will need to be overridden/altered so it switches
Drupal\redirect\Form\RedirectFormfor a different version.Ideally, the new regex functionality should be done in a way that doesn't hack the
redirect_source.pathto work.Given, how easy the implementation seems to be, it would be nice if this was actually supported in the original
redirectmodule as a first class citizen. e.g.1. with a new
add regex redirectspermission to restrict who can create/update regex redirects.2. introduce a new boolean field to flag if a URL is a regex based one or not.
Comment #5
codebymikey commentedI've got something sandboxed locally with the
?fixes and permission restriction, just assigning to myself so no-one else works on it in the meantime.It'll keep the
regex:approach, without introducing a new boolean base entity field (as that's out of scope for the issue)Will push up once the codes cleaned up.
Comment #6
o'briatSeems fine to me.
You could remove the
Escape forward slashes as \/from_redirect_regex_add_help_text()in the module file, since you use braces ({ }) as delimiterComment #7
o'briatHere is a MR with add the support of querystrings.
Comment #8
codebymikey commentedThat's all done already in my local version. Was just finalizing an integration test with the #2879648: Redirects from aliased paths aren't triggered feature since the module already had code trying to support it.
I don't think the commit addresses the original issue I mentioned with regards to using
?directly within a regex.I've already addressed the original encoding issue locally, and will push the changes up soon.
I do think adding support for matching against query strings is also useful, so will try and incorporate some of that into my version too.
I also like the addition of the views field filter, as I believe that's the probably the preferred way to integrate the module without messing with the site's existing configuration - that way the redirect_regex module can be easily uninstalled if needed without any lingering side-effects or broken configurations.
Comment #9
codebymikey commentedPushed the updated code up including functional and kernel tests addressing all the issues found during my tests.
Overview of current functionality:
1. Replace regex references.
2. Support query strings.
3. Provide permissions so only certain users can work with the regexes - #3578020: Introduce permission to restrict who can add/update regex permissions
4. Avoid manually specifying the
regex:orregex:qsprefix in the UI, there's now checkboxes to provide the functionality.Longer goals:
1. Migrate away from the
regex:prefix, and use fields (low priority)2. Try and optimize the repository so that the regex match happens on the database level. As it can be potentially very inefficient when the site has a lot of regex redirects.
Just need to see what the maintainer thinks. But I think I'm done with the issue for the time being as it does everything I'd like it to do.
But feel free to provide any feedback!
Comment #10
o'briatThe query part is not working as expected:
Redirection:
formation\?([^=]*).*$+ querystring +/formations/$1/For:
/formation?fooExpected:
/formations/foo/Observed:
/formations/foo/?foo=the querystring is appended to destination urlFor:
/formation?foo=barExpected:
/formations/fooObserved:
/formations/foo/?foo=barthe querystring is appended to destination urlI finally remember what I did to get rid of the querystring : uncheck the "redirect passthrough querystring" settings of the redirect module (global) or patch its file: /src/EventSubscriber/RedirectRequestSubscriber.php#L166
I believe there is a more elegant way to override
\Drupal\redirect\EventSubscriber\RedirectRequestSubscriber::onKernelRequestCheckRedirect()Comment #11
codebymikey commentedInitially planned to leave this for the specific sites to implement, but I've addressed the feedback from #10 as it does seem reasonable enough
Extra query strings added by the
passthrough_querystringsetting will be removed if the regex explicitly supports query strings. Then those requiring even more special handling of the query string can do so in their ownhook_redirect_response_alter()hooks.Comment #12
o'briatPerfect: thanks!
Comment #13
baikho commentedHey both, thanks a lot for the contributions here. I'm a bit busy lately, so I won't be able to follow up on this closely. If tests coverage is good I'm happy for this to go in.
Comment #15
codebymikey commentedThanks for testing @obriat! Committed.