Get internal path from external URL Is there a way to get Drupal internal path from the external URL? I need to get node id (or any other internal path) from the URL that user specifies in the textfield. External URL in this case is not the URL of current page, it is arbitrary URL that is entered by user.

For example:

Base URL: http://www.example.com/drupal

Alias of node/101: foo/bar

User input: http://www.example.com/drupal/foo/bar or http://www.example.com/drupal/?q=foo/bar

Expected result: node/101 or foo/bar

The problem explained:


drupal_get_normal_path() only returns normal path if internal alias is given. In this case internal alias from external URL needs to be extracted but there are no internal Drupal function that would do that directly.
If site has Clean URLs enabled, request path in external URL can either be in "q" argument (?q=foo/bar) or in URL itself (/drupal/foo/bar).
Dull Ax....

$path='';
$with_trailing_slash=$base_url . '/';

if (strpos($user_input, $with_trailing_slash)===0) {
  $user_input=str_replace($with_trailing_slash, '', $user_input);
  if (strpos($user_input, '?q=')===0) {
    $path=substr($user_input, 3);
  } else {
    $path=$user_input;
  }
} else {
  // this isn't on the local system so there is no way to figure it out
}

if ($path) {
  $normal=drupal_get_normal_path($path);
}