Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jandk4014's avatar

Redirect prevent [ ] encoding

We're building a site that uses arrays in the query string. During certain pages there are redirects to named routes with the query string. The problem that I have is that the redirect is encoding the query string that [ ]s and breaking functionality on the subsequent pages.

How can I prevent the query string from being encoded on the redirect?

0 likes
8 replies
biishmar's avatar

@jandk4014 use urldecode() function before appending query string and after u getting query string...

1 like
jandk4014's avatar

I'm a bit perplexed by the suggestion. I was attempting to do that but I think that I'm missing something. A small snippet of what I'm doing is as follows:

return redirect()->route('catalog.partType', ['partType'=>$partType, urldecode($this-request->getQueryString())]);

@biishmar - Is this what you were thinking about?

biishmar's avatar

@jandk4014

You tried decoding before sedning, why dont you try decoding at catalog.partType named route controller.. somtimes laravel encode code in the querystring after redirect...

jandk4014's avatar

@biishmar

It would appear that the queryString is decoding it correctly on the dump. But when the page is redirected with Laravel's functionality, the queryString is being encoded.

Thoughts?

jandk4014's avatar

Well after some hunting around the framework I found that there's a function that is ultimately being called that's encoding the parameters.

/**
     * Characters that should not be URL encoded.
     *
     * @var array
     */
    public $dontEncode = [
        '%2F' => '/',
        '%40' => '@',
        '%3A' => ':',
        '%3B' => ';',
        '%2C' => ',',
        '%3D' => '=',
        '%2B' => '+',
        '%21' => '!',
        '%2A' => '*',
        '%7C' => '|',
        '%3F' => '?',
        '%26' => '&',
        '%23' => '#',
        '%25' => '%',
    ];

    /**
     * Create a new Route URL generator.
     *
     * @param  \Illuminate\Routing\UrlGenerator  $url
     * @param  \Illuminate\Http\Request  $request
     * @return void
     */
    public function __construct($url, $request)
    {
        $this->url = $url;
        $this->request = $request;
    }

    /**
     * Generate a URL for the given route.
     *
     * @param  \Illuminate\Routing\Route  $route
     * @param  array  $parameters
     * @param  bool  $absolute
     * @return string
     *
     * @throws \Illuminate\Routing\Exceptions\UrlGenerationException
     */
    public function to($route, $parameters = [], $absolute = false)
    {
        $domain = $this->getRouteDomain($route, $parameters);

        // First we will construct the entire URI including the root and query string. Once it
        // has been constructed, we'll make sure we don't have any missing parameters or we
        // will need to throw the exception to let the developers know one was not given.
        $uri = $this->addQueryString($this->url->format(
            $root = $this->replaceRootParameters($route, $domain, $parameters),
            $this->replaceRouteParameters($route->uri(), $parameters)
        ), $parameters);

        if (preg_match('/\{.*?\}/', $uri)) {
            throw UrlGenerationException::forMissingParameters($route);
        }

        // Once we have ensured that there are no missing parameters in the URI we will encode
        // the URI and prepare it for returning to the developer. If the URI is supposed to
        // be absolute, we will return it as-is. Otherwise we will remove the URL's root.
        $uri = strtr(rawurlencode($uri), $this->dontEncode);

        if (! $absolute) {
            return '/'.ltrim(str_replace($root, '', $uri), '/');
        }

        return $uri;
    }

I guess that I need to change the .JS file to account for the encoded URIs.

lara168466's avatar

I was looking into the same problem. Sadly this is not easily extendable. I maybe later will look into making a PR on Laravel. For now i just used this hack. I would advise you to run this code only on your own generated URL's not on any user input.

    $route = route('url', ['query' => 'example']);
	$decoded = str($routeUrl)
            ->replace('%5B', '[')
            ->replace('%5D', ']')
            ->value();

Please or to participate in this conversation.