I have this code:

$event->setResponse(new RedirectResponse(base_path().'mypage#actions_clicker'));

It redirects to /mypage, but it doesn not include the #actions_clicker part. How do I make Drupal not loose the hash / # part?


Try url encoding the # fragment. so it becomes %23, e.g:

$event->setResponse(new RedirectResponse(base_path().'mypage%23actions_clicker'));


Check the browser header to see that the fragment is passed. In my local testing it was ok:

Sample code to append fragment to redirect response.

return new RedirectResponse($redirect_url . '#test', 301);

Location header includes fragment

More info: https://stackoverflow.com/a/2305927/583715

  • Says "Access denied". I.e. Drupal doesnt like that path. – coderama Mar 11 '19 at 11:00
  • Ah ok, urlencoding # to %23 has worked for a couple of cases for me. Recently, when encoding the destination parameter. Generally # should be valid in the redirect URL, as per the http spec, but you may need to inspect the request with curl or similar to check the http requests and responses + headers there. In my local test, I could also pass #actions_clicker from RedirectResponse ok there – David Thomas Mar 11 '19 at 11:04

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.