Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable strip query string for regex or selectable per redirection #184

Open
ilicmarko opened this issue Jun 18, 2021 · 6 comments
Open

Enable strip query string for regex or selectable per redirection #184

ilicmarko opened this issue Jun 18, 2021 · 6 comments
Labels
enhancement New feature or request

Comments

@ilicmarko
Copy link

Is your feature request related to a problem? Please describe.

If you have a regexmatch you usually get the whole match. For example:

From To
/old(.*) /new$1

This would essentially remap all requests from old. The "problem" is if you have Strip Query String from 404s turned off, which usually is the case, you would get duplicate query string.

Describe the solution you would like

There are essentially three solution that come to mind at the moment:

  • Always strip params for regex match (maybe problematic for backwards compatibility)
  • Add a global settings to enable stripping for regex (essentially splitting the current option into two).
  • Add the option for each redirect. Therefore it can be chosen from redirect to redirect.

Describe alternatives you have considered

The idea was to maybe capture everything up till the ? but that maybe problematic if there are anchors.

([^?]+)e.

@ilicmarko ilicmarko added the enhancement New feature or request label Jun 18, 2021
@khalwat
Copy link
Contributor

khalwat commented Jun 18, 2021

We can't reliably strip a query string from a regex match, because the ? has special meaning in a RegEx.

Also yes, it would break backwards compatibility.

You can come up with a RegEx that will match everything up to the next ? though, which would effectively strip the query string as well. If you do care about anchors, you can have another capture group that captures any anchors, and combine them for the final URL.

@khalwat khalwat closed this as completed Jun 18, 2021
@ilicmarko
Copy link
Author

ilicmarko commented Jun 18, 2021

My ideas was not to strip query string from Regex it was to disable the option of adding (appending) them to the URL for certain redirects.

Global Settings: Strip Query String from 404s false

Original From To Strip Final
domain.com/foo?gclid=1 /foo /bar false (default) domain.com/bar?gclid=1
domain.com/foo?gclid=1 /foo /bar true domain.com/bar
domain.com/foo?gclid=1 /foo(.*) /bar$1 false (default) domain.com/bar?gclid=1?gclid=1
domain.com/foo?gclid=1 /foo(.*) /bar$1 true domain.com/bar?gclid=1

It would impact this if, where you can check the value for each redirection individually.

if (Retour::$settings->preserveQueryString) {
$request = Craft::$app->getRequest();
if (!empty($request->getQueryStringWithoutPath())) {
$dest .= '?' . $request->getQueryStringWithoutPath();
}
}

@khalwat

Thinking about the solution with regex you wrote that might not be possible. Because you are appending the query string and if I append the anchor the query string will always come after the anchor which is not valid.

/foo/bar?hello#world -> I capture the parts in regex -> /baz/bar/#world -> Retour appends query /baz/bar/#world?hello.

Making this impossible to achieve. This is way I would like to Enable stripping per route. Because in most cases I don't need it but in (almost all) regex cases I do need it.

@khalwat
Copy link
Contributor

khalwat commented Jun 18, 2021

Ah I see, sorry I misunderstood. So you want that option to have a per-redirect override, essentially?

@khalwat khalwat reopened this Jun 18, 2021
@ilicmarko
Copy link
Author

Exactly.

@jan-dh
Copy link

jan-dh commented Aug 4, 2022

Would love something like this as well, would be very handy to pass around UTM tags etc. from a marketing perspective

@Abromeit
Copy link

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants