12 March 2025

In Drupal 7, request_path() is often used to get the request URI. However, this function relies on $_GET['q'], which may not always reflect the actual request URI shown in the browser, especially when clean URLs are disabled.

To ensure we get the correct request path directly from the browser's URI, we can use the request_uri() function instead.

Source code viewer
  1. // Get the full request URI.
  2. $request_uri = request_uri();
  3.  
  4. // Extract the path component without query parameters.
  5. $request_path = rtrim(strtok($request_uri, '?'), '/');
  6.  
  7. // Output or use the request path as needed.
  8. watchdog('custom', 'Current request path: @path', array('@path' => $request_path), WATCHDOG_NOTICE);
Programming Language: PHP