If you use a form with this redirect:
$form_state['redirect'] = "somepath/somepage#section2";

you will instead be taken to:
http://example.com/somepath/somepage%23section2

Thanks.
-Mitch Schwenk

Comments

tazzytazzy’s picture

I think I fixed this. Sorry, if this is not in the correct patch format. This is from includes/common.inc, here's an updated drupal_goto() function:

function drupal_goto($path = '', array $options = array(), $http_response_code = 302) {
  // A destination in $_GET always overrides the function arguments.
  // We do not allow absolute URLs to be passed via $_GET, as this can be an attack vector.
  $destination = array();
  if (isset($_GET['destination']) && !url_is_external($_GET['destination'])) {
    $destination = drupal_parse_url($_GET['destination']);
  } else {
    $destination = drupal_parse_url($path);
  }
  $path = $destination['path'];
  $options['query'] = $destination['query'];
  $options['fragment'] = $destination['fragment'];

  drupal_alter('drupal_goto', $path, $options, $http_response_code);

  // The 'Location' HTTP header must be absolute.
  $options['absolute'] = TRUE;

  $url = url($path, $options);

  header('Location: ' . $url, TRUE, $http_response_code);

  // The "Location" header sends a redirect status code to the HTTP daemon. In
  // some cases this can be wrong, so we make sure none of the code below the
  // drupal_goto() call gets executed upon redirection.
  drupal_exit($url);
}

Mitch Schwenk

bfroehle’s picture

Category: bug » support
Status: Active » Fixed

Your proposed patch in #1 would unfortunately break a lot of other functionality in Drupal, as it'd be impossible to pass in $options['query'] or $options['fragment'].

However, you can accomplish your goal without any patches by setting

$form_state['redirect'] = array('somepath/somepage', array('fragment' => 'section2'));

This advanced form of redirect is only used a few times in Drupal core, see path_admin_form_delete_submit() for example.

tazzytazzy’s picture

Thanks. That works.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

dunx’s picture

I'm theming some flag_clear module links from a nodecomment under advanced_forum. I was expecting to be able to use:

:
'query' => 'destination=node/'.$parent_nid,
'fragment' => 'comment-'.$nid,
:

The URL looks fine on the form page (http://mysite.com/flag/reset/abuse_node/9619?destination=node/9618#comme...), but it redirected back to the parent URL mysite.com/node/9618 (well, the URL alias of that page anyway) and effectively ignored the fragment. I'm assuming this is because the fragment is assumed to be for the flag/reset page, rather than retained for the destination page.

However, the following did work:

'query' => 'destination=node/'.$parent_nid.'%23comment-'.$nid,

Probably the same with normal comments.

This is all under D6 BTW! Bug worth raising?