3

I have module where I'm creating a custom path in hook_menu. When the user visits this page, they are supposed to be redirected to another site, with GET query parameters attached to the redirect URL. So for instance, if they visit http://mysite.com/site-search?query=blah, we are supposed to redirect them to http://othersite.com/different-path?query=blah.

However, we have clean URLs enabled; while I can hit the page http://mysite.com/site-search, it gives a page not found when I try to hit http://mysite.com/site-search?query=blah.

The requested page "/site-search?query=blah" could not be found.

I tried adding another path for site-search/%, just to get that extra part of the URL, but that didn't work either.

Am I able to get the URL parameters while using clean URLs?

Edit: Here's the code:

function my_redirect_menu() {
  $items['site_search'] = array(
    'page callback' => 'my_redirect_redirect',
    'access callback' => TRUE, 
  );

  // testing to see if this will allow me to capture get parameters
  // in the form http://site.com/site_search/q=blah
  $items['site_search/%'] = array(
    'page callback' => 'my_redirect_redirect',
    'access callback' => TRUE, 
  );

  return $items;
}

function my_redirect_redirect() {
  // see if we can get the get paramters
  return "<pre>" . print_r($_SERVER,1) . "</pre>";
}

Edit 2 In my example, I transcribed the query string in correctly. It looks like it's a conflict between drupal wanting to use ?q= for its menu routing, and our search host URLs also using ?q= for their queries. If I do query=blah, I am able to hit the page.

2
  • May you show the code used by the module? Without seeing it, it is not possible to say what is wrong. I am not clear why you are making an example for mysite.com/site-search?query=blah, but the error message is for /site-search?q=blah.
    – avpaderno
    Commented Nov 7, 2012 at 19:47
  • @Kiamlaluno in writing this question I transcribed my query strings incorrectly. It appears to be a conflict with both the drupal site and our search host wanting to use ?q=.
    – user1359
    Commented Nov 7, 2012 at 19:58

2 Answers 2

4

I use $_GET variables as needed in my clean-url installs without issue. Views uses $_GET variables, too. Drupal uses $_GET['q'] so you don't want to use that ID, but you should be able to do something like:

function my_redirect_menu() {
  $items['site_search'] = array(
    'page callback' => 'my_redirect_redirect',
    'access callback' => TRUE, 
    'type' => MENU_CALLBACK,
  );
  return $items;
}

function my_redirect_redirect() {
  return $_GET['query'];
}

This is working for me locally. The 2nd item you present in hook_menu only matches for /site_search/foo?query=bar so you only want the first. Be sure to set the 'type' property as well.

1

Sorry to be VERY late to the party. For anybody still looking for the answer:

function my_redirect_menu() {
    $items['site_search/%'] = array(
      'title' => 'Edit Product Details',
      'page callback' => 'my_redirect_redirect',
      'page arguments' => array(1),
      'access arguments' => array('access content')
    );
}

function my_redirect_redirect($id) {
   return t("your url % value was:".$id);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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