def redirect(pattern, to, permanent=True, locale_prefix=True, anchor=None, name=None,
             query=None, vary=None, cache_timeout=12, decorators=None, re_flags=None,
             to_args=None, to_kwargs=None, prepend_locale=True, merge_query=False):
    if permanent:
        redirect_class = HttpResponsePermanentRedirect
    else:
        redirect_class = HttpResponseRedirect
    if locale_prefix:
        pattern = pattern.lstrip('^/')
        pattern = LOCALE_RE + pattern
    if re_flags:
        pattern = '(?{})'.format(re_flags) + pattern
    view_decorators = []
    if cache_timeout is not None:
        view_decorators.append(cache_control_expires(cache_timeout))
    if vary:
        if isinstance(vary, basestring):
            vary = [vary]
        view_decorators.append(vary_on_headers(*vary))
    if decorators:
        if callable(decorators):
            view_decorators.append(decorators)
        else:
            view_decorators.extend(decorators)
    def _view(request, *args, **kwargs):
        kwargs = {k: v or '' for k, v in kwargs.items()}
        args = [x or '' for x in args]
        if callable(to):
            to_value = to(request, *args, **kwargs)
        else:
            to_value = to
        if to_value.startswith('/') or HTTP_RE.match(to_value):
            redirect_url = to_value
        else:
            try:
                redirect_url = reverse(to_value, args=to_args, kwargs=to_kwargs)
            except NoReverseMatch:
                redirect_url = to_value
        if prepend_locale and redirect_url.startswith('/') and kwargs.get('locale'):
            redirect_url = '/{locale}' + redirect_url.lstrip('/')
        if args or kwargs:
            redirect_url = strip_tags(force_text(redirect_url).format(*args, **kwargs))
        if query:
            if merge_query:
                req_query = parse_qs(request.META.get('QUERY_STRING'))
                req_query.update(query)
                querystring = urlencode(req_query, doseq=True)
            else:
                querystring = urlencode(query, doseq=True)
        elif query is None:
            querystring = request.META.get('QUERY_STRING')
        else:
            querystring = ''
        if querystring:
            redirect_url = '?'.join([redirect_url, querystring])
        if anchor:
            redirect_url = '
        if PROTOCOL_RELATIVE_RE.match(redirect_url):
            redirect_url = '/' + redirect_url.lstrip('/')
        return redirect_class(redirect_url)
    try:
        for decorator in reversed(view_decorators):
            _view = decorator(_view)
    except TypeError:
        log.exception('decorators not iterable or does not contain '
                      'callable items')
    return url(pattern, _view, name=name)