Redirect query parameters

49 views
Skip to first unread message

Jeffrey Eliasen

unread,
Feb 3, 2018, 2:52:05 PM2/3/18
to Django developers (Contributions to Django itself)
Right now the `django.contrib.redirects` app only redirects the URL path, it ignores query parameters. I believe it should be optional for the user to pass-through query parameters in the redirect; this is especially important for marketing campaigns where query parameters are used to identify the source of traffic.

I am happy to do the work, but want to make sure the changes I am proposed are considered the right way to do this.

I believe this should work as follows:
  1. with every redirect, the admin has the ability to optionally include query parameters, with a second option for query parameter re-writes described below
  2. *if* query parameters are being passed through:
    1. if there are no query parameters in the `new_path` field, query parameters are passed through unmodified
    2. if there are query parameters in the `new_path` field and override parameters is set, then passed query parameters are replaced with the provided parameters in the `new_path` (parameters not specified in `new_path` are still passed through untouched)
    3. if there are query parameters in the `new_path` field and override parameters is unset, then passed query parameters replace the provide parameters in `new_path`

As a simple example of 2.2:
old_path = '/abc/', new_path = '/xyz/?x=1', request = '/abc/?x=2&y=2' -> redirects to '/xyz/?x=1&y=2'

As a simple example of 2.3:
old_path = '/abc/', new_path = '/xyz/?x=1', request = '/abc/?x=2&y=2' -> redirects to '/xyz/?x=2&y=2'

I propose the following changes:
  • add an optional `include_query_parameters` boolean to the Redirect model, default=False
    • this guarantees all existing and newly-created redirects are unaffected by the new behavior
  • add an optional `overwrite_parameters` boolean to the Redirect model, default=True
    • this guarantees all newly-created redirects default to passing specified parameters if the user sets `include_query_parameters` to True
  • modify the middleware to implement the behaviors described above

Thanks for feedback and suggestions!

jeff

Reply all
Reply to author
Forward
0 new messages