Skip to main content

URL redirecting and rewriting in code.

Project description

# django-redirect-urls

[![Build Status](https://travis-ci.org/pmac/django-redirect-urls.svg?branch=master)](https://travis-ci.org/pmac/django-redirect-urls)

Manage URL redirects and rewrites in Django like you do other URLs: in code.

This was extracted from [bedrock](https://github.com/mozilla/bedrock/) (the code behind www.mozilla.org).
We had a lot of redirects and rewrites we wanted to move out of Apache configs and into versioned code.
This library is the result of all of that. Because it started life on a Mozilla site it does have some
features for how we make sites (e.g. locale prefixing in URLs and the `is_firefox` helper). Now that it
is a separate thing however I'm very open to adding features and helpers for a more general audience.

## Install

```bash
$ pip install django-redirect-urls
```

## Examples

With this library you can do things like:

```python
# urls.py
from redirect_urls import redirect

urlpatterns = [
redirect(r'projects/$', 'mozorg.product'),
redirect(r'^projects/seamonkey$', 'mozorg.product', locale_prefix=False),
redirect(r'apps/$', 'https://marketplace.firefox.com'),
redirect(r'firefox/$', 'firefox.new', name='firefox'),
redirect(r'the/dude$', 'abides', query={'aggression': 'not_stand'}),
]
```

There are a lot of options to the `redirect` helper. Here is the basic list:

* **pattern**: the regex against which to match the requested URL.
* **to**: either a url name that `reverse` will find, a url that will simply be returned,
or a function that will be given the request and url captures, and return the
destination.
* **permanent**: boolean whether to send a 301 or 302 response.
* **locale_prefix**: automatically prepend `pattern` with a regex for an optional locale
in the url. This locale (or None) will show up in captured kwargs as 'locale'.
* **anchor**: if set it will be appended to the destination url after a '#'.
* **name**: if used in a `urls.py` the redirect URL will be available as the name
for use in calls to `reverse()`. Does _NOT_ work if used in a `redirects.py` file.
* **query**: a dict of query params to add to the destination url.
* **vary**: if you used an HTTP header to decide where to send users you should include that
header's name in the `vary` arg.
* **cache_timeout**: number of hours to cache this redirect. just sets the proper `cache-control`
and `expires` headers.
* **decorators**: a callable (or list of callables) that will wrap the view used to redirect
the user. equivalent to adding a decorator to any other view.
* **re_flags**: a string of any of the characters: "iLmsux". Will modify the `pattern` regex
based on the documented meaning of the flags (see python re module docs).
* **to_args**: a tuple or list of args to pass to reverse if `to` is a url name.
* **to_kwargs**: a dict of keyword args to pass to reverse if `to` is a url name.
* **prepend_locale**: if true the redirect URL will be prepended with the locale from the
requested URL.
* **merge_query**: merge the requested query params from the `query` arg with any query params
from the request.

Or you can install the `redirect_urls.middleware.RedirectsMiddleware` middleware and create
`redirects.py` files in your Django apps. This will allow you to define a lot of redirects
in their own files (which will be auto-discovered) and guarantee that they'll be tested before
the rest of your URLs.

```python
# redirects.py
from redirect_urls import redirect

redirectpatterns = [
redirect(r'projects/$', 'mozorg.product'),
redirect(r'^projects/seamonkey$', 'mozorg.product', locale_prefix=False),
redirect(r'apps/$', 'https://marketplace.firefox.com'),
redirect(r'firefox/$', 'firefox.new', name='firefox'),
redirect(r'the/dude$', 'abides', query={'aggression': 'not_stand'}),
]
```

## Run The Tests

```bash
$ pip install tox
$ tox
```

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-redirect-urls-0.2.tar.gz (11.4 kB view details)

Uploaded Source

Built Distribution

django_redirect_urls-0.2-py2.py3-none-any.whl (14.8 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django-redirect-urls-0.2.tar.gz.

File metadata

File hashes

Hashes for django-redirect-urls-0.2.tar.gz
Algorithm Hash digest
SHA256 3c765f0791ade7c5814089425dd095af9ff18d56026f233774744d27103441db
MD5 6c2303601f8e4a08292d4887760fcad7
BLAKE2b-256 f42eea3fac18215312846abeec9deee9d47856bdc89103a2c03c3a6622d0c8c9

See more details on using hashes here.

File details

Details for the file django_redirect_urls-0.2-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_redirect_urls-0.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 bb8806327e0a5c9c98393836e6339d374d56ec0a95b945b67ec9650d4ae27638
MD5 6c96df62f994374205beb8ca23039a8b
BLAKE2b-256 9bf57a162dc807b3c1564f71a21d4f07cd5ee86d8bd2f2ac78e2c145d9a909d7

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page