This works:

$redirectURL = '/cart/add/p2';
return new RedirectResponse(base_path().$redirectURL);

The problem is, I want this:

$redirectURL = '/cart/add/p2?destination=somewhere';

The moment I added destination=somewhere, I get this error:

InvalidArgumentException: The internal path component 'http://localhost/XXXX/public_html/XXXX/XXXX' is invalid. Its path component must have a leading slash, e.g. internal:/foo. in Drupal\Core\Url::fromInternalUri() (line 404 of C:\ampps\www\XXXX\public_html\core\lib\Drupal\Core\Url.php).

However, if I do this:

$redirectURL = '/cart/add/p2?SOMETHING_ELSE=somewhere';

Then it works. In other words, it specifically breaks if I add "destination=somewhere".

Why would it be breaking on this and how do I get around it? I.e. How do I redirect to a URL with destination=somewhere in the query string.

FYI: If I can get this to work, I can use "Cart Links" to add stuff to a cart and redirect the user at the same time, making my live a lot easier.

4 Answers 4


Better use a route, so that the path processing in Drupal can do its job. If custom.cart_add is the route to /cart/add/{page}, this returns a redirect response with a destination query parameter from a controller:

  return $this->redirect(
    ['page' => 'p2'],
      'query' => ['destination' => 'somewhere'],
      'absolute' => TRUE,
  • This only works in the context of a controller, which isnt always the case (e.g. I am handling onKernelRequest)
    – Jeff
    Dec 18, 2018 at 17:32
  • @Jeff, the question was about returning a response from a controller. In an event subscriber you can create a new RedirectResponse like in the other answers, but don't send the response, set it in the event: $event->setResponse()
    – 4uk4
    Dec 18, 2018 at 17:47

Try this code.

$path = Drupal\Core\URL::fromUserInput('/cart/add/p2', array('query' => array('SOMETHING_ELSE' => 'somewhere')))->toString();
$response = new RedirectResponse($path);

  • Same problem. That toString just turns my string into one that has "destination=bla", and the moment you have "destination=bla" in the "new RedirectResponse" bit, it breaks.
    – coderama
    Feb 7, 2018 at 5:16
  • @coderama Try updated code and let me know if it is not working with the error message. Feb 8, 2018 at 11:39
  • Please note case sensitivity on the class name. It should be "Drupal\Core\Url::" Mar 13, 2020 at 18:33

This full example might help:

use Symfony\Component\HttpFoundation\RedirectResponse;

$target = 65; // a parameter you want to add
$url = Url::fromUri('internal:/node/add/page'); // choose a path
$link_options = array(
  'attributes' => array(
     'class' => array(
   'query' => array(
     'target' => $target
$destination = $url->toString();

$response = new RedirectResponse($destination);
$redirectURL = '/cart/add/p2';
return new RedirectResponse(base_path().$redirectURL);

The code above produces double slash before the cart item, because base_path() always adds a slash at the end of the returned path. You need to remove the slash from $redirectURL variable to get it work correctly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.