Is it somehow possible to change the path the search uses from, let's say

search/node/keyword

to

search/keyword

or even

something/keyword?

Comments

Jimboy’s picture

Still really need this.
If the solution isn't simple, I'd be glad for a hint into the right direction!

Ayesh’s picture

Install the Custom search module and set the path to
/search/

you can add your site's search for a custom search in browser's custom search using

www.example.com/search/%s

most browsers use the %s as the search query. Use %q it fails.

Jimboy’s picture

Thank you very much for your reply!

I installed the said module, but I can't seem to make it work. When using my search box, after clicking 'submit' the path is the one I want (for example 'mysearch/keyword'). But Drupal tells me the site wasn found.
So the form just posts to another URL, but there's nothing there to listen for it.

Any further tips?

Ayesh’s picture

Does your Core search work ?

If yes , then enable custom search and configure it.

Admin> config > custom search

then, change the search URL

make sure that you use simple letters

Jimboy’s picture

Yes, my core search works.

I already followed all the steps you described. If I search for something after having set up the custom path, I receive a "page not found" error.

kalis1’s picture

Hello,
I was encountering the same issue and discovered this topic : http://drupal.org/node/887276, and the short answer is : "you should only use this feature if you're not using the default Drupal core search module."

So could anyone tell us how to change the core search path ?

Thanks in advance...

scottalan’s picture

Not sure if you ever got the answer you sought, but I thought I would post just in case anyone else was searching for the same answer.

In your theme's template.php you can set up a completely customized search that's fairly simple.

First I needed a $variable that I could use in a custom .tpl file. I also needed to only search in a specific content type(book). So in my theme's template.php file I added the following functions:

/**
 * Override or insert variables into the node templates.
 *
 * @param $vars
 * An array of variables to pass to the theme template.
 */
function YOURTHEME_preprocess_node(&$vars) {
  if ($vars['node']->type == 'book') {
    $vars['book_search'] = drupal_get_form('YOURTHEME_custom_search_form');
  }
}
/**
 * A custom search form.
 *
 * @param $form_state
 * @param $keys
 * The search string entered by the user, containing keywords for the search.
 * @return $form
 * A Form API array for the search form.
 */
function YOURTHEME_custom_search_form(&$form_state, $keys = '') {
  // form to search a given content type
  if (module_exists('search')) {
    $form = search_form($form_state, '/' . drupal_get_path_alias($_GET['q']));
    $form['#validate'] = array('search_form_validate', 'YOURTHEME_custom_search_validate');  // We will add some additional validation
    $form['#submit'] = array('search_form_submit');
    return $form;
  }
}
/**
 * A validate function for YOURTHEME_custom_search_form.
 *
 * @param $form
 *
 * @param array $form_state
 */
function YOURTHEME_focused_search_validate($form, &$form_state) {
  // Determine which content type to search based on path
  $content_type = array_search($form['#action'], array(
      // If this path, search the content type 'book'....
      'book' => '/help/book/user-guide',  // Path to a page that you are viewing the 'book' content type at.
  ));
  $content_type = empty($content_type) ? '' : $content_type;
  $keys = $form_state['values']['keys'];
  $keys = search_query_insert($keys, 'type', $content_type);
  form_set_value($form['basic']['inline']['processed_keys'], trim($keys), $form_state);
}

Remember the $variable we created in the _node_preprocess function...

To actually display the custom search_form you need to add the following in your .tpl file. Keep in mind that I needed this for a specific node->type therefore I added this to node-book.tpl.php in my theme folder. Make sure if you are using a custom theme (sub-theme) you have a copy of node.tpl.php in your theme folder as well.

print $book_search

-Scott

merzikain’s picture

I have another solution but it requires you either add to or create your own module.

I created a module that contains a set of functions I use on every site I work on so I just added this to that but it could easily be used in a separate module.

First you need a menu item in your module's hook_menu() function based off of the current search.module menu item:

// search page
if(!function_exists('search_view')) require_once($_SERVER['DOCUMENT_ROOT'].str_replace('/',DIRECTORY_SEPARATOR,base_path() . drupal_get_path('module','search').'/search.pages.inc'));
$path = drupal_get_path('module','search').'/search.pages.inc';
$base = drupal_get_path('module',YOURMODULENAME);
$set = explode('/',$base);
for($i = 0, $back = ''; $i < sizeof($set); $i++){
	$back .= '../';
}
$items['search/%menu_tail'] = array(
	'title callback' => 'module_invoke',
	'title arguments' => array('node', 'search', 'name', TRUE),
	'page callback' => 'YOURMODULENAME_search_view',
	'page arguments' => array('node'),
	'access callback' => '_search_menu',
	'access arguments' => array('node'),
	'type' => MENU_LOCAL_TASK,
	'parent' => 'search',
	'file' => $back.$path,
);

Note that the $back and $_SERVER['DOCUMENT_ROOT'] portions of the file path are necessary for my serving environment and current Drupal setup for the site I am using this on. You may need to modify (or remove) that part for your own. You'll know if you do if you get the "cannot find blah blah blah" error that will get thrown at you otherwise.

Next we create (or add to) the YOURMODULENAME_init() function:

if($uri['args'][0] == 'search'){
	if($uri['args'][1] == 'node' && $uri['args'][2]) drupal_goto($uri['args'][0].'/'.$uri['args'][2]);
}

I have a function that returns a lot of useful information about the current page's uri that I use to populate $uri. You can create your own array to fit this by adding the following line before this code:

$uri = array('args' => explode('/',substr($_SERVER['REQUEST_URI'],1,strlen($_SERVER['REQUEST_URI']))));

I find that sometimes when using arg() I get Drupal node path arguments (node/1) instead of the actual uri being shown to the user. That was my reason for creating the $uri thing, more portable.

Anyway, next we need the YOURMODULENAME_search_view function:

function YOURMODULENAME_search_view() {
  // Search form submits with POST but redirects to GET. This way we can keep
  // the search query URL clean as a whistle:
  // search/type/keyword+keyword
  if (!isset($_POST['form_id'])) {
    $keys = YOURMODULENAME_search_get_keys();
    // Only perform search if there is non-whitespace search term:
    $results = '';
    if (trim($keys)) {
      // Log the search keys:
      watchdog('search', '%keys (@type).', array('%keys' => $keys, '@type' => module_invoke('node', 'search', 'name')), WATCHDOG_NOTICE, l(t('results'), 'search/'. $keys));

      // Collect the search results:
      $results = YOURMODULENAME_search_data($keys);

      if ($results) {
        $results = theme('box', t('Search results'), $results);
      }
      else {
        $results = theme('box', t('Your search yielded no results'), search_help('search#noresults', drupal_help_arg()));
      }
    }

    // Construct the search form.
    $output = drupal_get_form('search_form', NULL, $keys, $type);
    $output .= $results;

    return $output;
  }

  return drupal_get_form('search_form', NULL, empty($keys) ? '' : $keys, 'node');
}

Then the YOURMODULENAME_search_get_keys() and YOURMODULENAME_search_data() functions:

function YOURMODULENAME_search_get_keys() {
	// Extract keys as remainder of path
	// Note: support old GET format of searches for existing links.
	$path = explode('/', $_GET['q'], 2);
	$keys = empty($_REQUEST['keys']) ? '' : $_REQUEST['keys'];
	$return = count($path) == 2 ? $path[1] : $keys;
	return $return;
}

function YOURMODULENAME_search_data($keys = NULL) {
  if (isset($keys)) {
    if (module_hook('node', 'search')) {
      $results = module_invoke('node', 'search', 'search', $keys);
      if (isset($results) && is_array($results) && count($results)) {
        if (module_hook($type, 'search_page')) {
          return module_invoke('node', 'search_page', $results);
        }
        else {
          return theme('search_results', $results, 'node');
        }
      }
    }
  }
}

You should notice that the $type used in the search module's functions is set to a static value of 'node' throughout the modified functions. Which means you won't be able to use this for searching anything other than nodes. It suits me just fine since I don't want users searching for user accounts (most of the sites I work on do not have them).

If you didn't already know, the second uri argument in the search path tells the search module what type of info to look for.

To change where the search page is located you'll have to create the appropriate menu items and modify the above code to reflect the new location.

If you cannot (or do not want to) use the custom search module this may be a better solution for you. It works great for my purposes and eventually I'm going to setup an admin option to change the location to whatever is set in that option.

bcbg’s picture

Hi,

I have a similar question. Where can I change the search path from

search/node/keyword

to

search/?key=keyword ?

Can it be achieved by using the custom search module? Thanks!

tassaf’s picture

In drupal 7 it's as following:

search/user/[key]|User

rajat.dkte’s picture

Install pathauto module,
go to /admin/config/search/path/add
Add Existing system path "search/node"
Add Path alias, What ever you want, "other"

Deepesh151086’s picture

yes.. it's working... thanks

Deepesh151086’s picture

Yes, It worked like a charm.. Thanks

vishnu9609’s picture

It is working Fine.... Thanks...

Ashrafi’s picture

<?php
function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
		
  if ($form_id == 'search_block_form' || $form_id == 'search_form') {
    $form['#submit'][] = 'drupal_search_box_form_submit';
  }

}
//Modify drupal search path
function drupal_search_box_form_submit($form, &$form_state) {
	$info = search_get_default_module_info();
  if ($info) {

	if($form_state['values']['form_id']=='search_form'){
		$form_state['redirect'] = array('search/' . $info['path'] .'/', array(
			'query' => array(
			 'keys' => $form_state['values']['keys'],
			 
			)
		));
	}else{
		$form_state['redirect'] = array('search/' . $info['path'] .'/', array(
			'query' => array(
			 'keys' => $form_state['values']['search_block_form'],
			 
			)
		));
	}  

	
  }

}
?>