Forums

Change Search Permalink (13 posts)

  1. sac1978
    Member
    Posted 2 years ago #

    OK, I want to change the default domain.com/search/keyword to maybe domain.com/result/keyword.

    I can get the "search" based one to work by adding RewriteRule ^search/(.+)?$ /index.php?s=$1 [QSA,L] to my .htaccess file.

    Firstly I tried just changing the .htaccess file to RewriteRule ^result/(.+)?$ /index.php?s=$1 [QSA,L] to use the "result" but this was not enough.

    I also have to go to ../wp-includes/rewrite.php and change the following:

    /**
    	 * Search permalink base ( example.com/search/query ).
    	 *
    	 * @since 1.5.0
    	 * @access private
    	 * @var string
    	 */
    	var $search_base = 'search';
    
    to 
    
    	/**
    	 * Search permalink base ( example.com/search/query ).
    	 *
    	 * @since 1.5.0
    	 * @access private
    	 * @var string
    	 */
    	var $search_base = 'result';

    Go and save my permalinks again and now I can search using domain.com/result/keyword

    The issue I have is that this has been a change to the core files, I need to do this hopefully with a hook so the core code remains unchanged as a wordpress upgrade would over write these changes.

    Also is there a way to change the space in the search terms from domain.com/result/keyword1+keywords2 which uses a + to say domain.com/result/keyword1-keywords2 which uses a more seo friendly -

    Can anyone please help me out with this.

    Thanks

    Stephen.

  2. sac1978
    Member
    Posted 2 years ago #

    Can this be done?

  3. Mark / t31os
    Moderator
    Posted 2 years ago #

    Are none of the various filters throughout wp-includes/rewrite.php appropriate then? You've obviously been looking at the rewrite file, how come you missed all the filters available?

    Line 1605:

    $search_rewrite = apply_filters('search_rewrite_rules', $search_rewrite);
  4. sac1978
    Member
    Posted 2 years ago #

    I can cobble the code together most the time, but I dont understand how I would make the change using that filter?

    Can you give me any pointers?

  5. Mark / t31os
    Moderator
    Posted 2 years ago #

  6. sac1978
    Member
    Posted 2 years ago #

    Got that working, thanks for pointing me at filters :)

    Any ideas where the + which equals space gets striped out? so we have domains.com/result/term1+term2

    I would like to change that + to a - so we get domains.com/result/term1-term2

    Thanks

  7. Mark / t31os
    Moderator
    Posted 2 years ago #

    No idea, possibly somewhere inside the WP_Query object .. (wp-includes/query.php) or whichever function goes about setting up query vars..

  8. sac1978
    Member
    Posted 2 years ago #

    I thought I had this sorted, but ive come back to the site and the search remains as it was before, this is the code I had that I thought was working

    function ChangeSearchString($stext)
    {
    $result = $stext;
    $result = str_replace("search", "result", $result);
    return $result;
    }   
    
    add_filter('search_rewrite_rules', 'ChangeSearchString');
  9. Mark / t31os
    Moderator
    Posted 2 years ago #

    The incoming variable $stext in an array of data, not a string, the str_replace won't fulfil the requirement.

    Try this instead.

    function ChangeSearchString( $search_rewrite ) {
    	if( !is_array( $search_rewrite ) )
    		return $search_rewrite;
    
    	$new_array = array();
    
    	foreach( $search_rewrite as $pattern => $_s_query_string )
    		$new_array[ str_replace( 'search/', 'result/', $pattern ) ] = $_s_query_string;
    
    	$search_rewrite = $new_array;
    
    	unset( $new_array );
    
    	return $search_rewrite;
    }
    
    add_filter('search_rewrite_rules', 'ChangeSearchString');

    Remember to click Save on the permalinks page after adding the new code... ;)

    Hope that helps...

  10. sac1978
    Member
    Posted 2 years ago #

    ok that code is now working. Thanks

    Any ideas on how I would go about getting these rules into wordpress as they have no effect in the htaccess file.

    RewriteRule ^morepages/([^/]+)-([^/]+)-([^/]+)-([^/]+)-([^/]+)-([^/]+)-([^/]+)-([^/]+)-([^/]+)-([^/]+).html /index.php?s=$1+$2+$3+$4+$5+$6+$7+$8+$9+$10 [QSA,L]
    RewriteRule ^morepages/([^/]+)-([^/]+)-([^/]+)-([^/]+)-([^/]+)-([^/]+)-([^/]+)-([^/]+)-([^/]+).html /index.php?s=$1+$2+$3+$4+$5+$6+$7+$8+$9 [QSA,L]
    RewriteRule ^morepages/([^/]+)-([^/]+)-([^/]+)-([^/]+)-([^/]+)-([^/]+)-([^/]+)-([^/]+).html /index.php?s=$1+$2+$3+$4+$5+$6+$7+$8 [QSA,L]
    RewriteRule ^morepages/([^/]+)-([^/]+)-([^/]+)-([^/]+)-([^/]+)-([^/]+)-([^/]+).html /index.php?s=$1+$2+$3+$4+$5+$6+$7 [QSA,L]
    RewriteRule ^morepages/([^/]+)-([^/]+)-([^/]+)-([^/]+)-([^/]+)-([^/]+).html /index.php?s=$1+$2+$3+$4+$5+$6 [QSA,L]
    RewriteRule ^morepages/([^/]+)-([^/]+)-([^/]+)-([^/]+)-([^/]+).html /index.php?s=$1+$2+$3+$4+$5 [QSA,L]
    RewriteRule ^morepages/([^/]+)-([^/]+)-([^/]+)-([^/]+).html /index.php?s=$1+$2+$3+$4 [QSA,L]
    RewriteRule ^morepages/([^/]+)-([^/]+)-([^/]+).html /index.php?s=$1+$2+$3 [QSA,L]
    RewriteRule ^morepages/([^/]+)-([^/]+).html /index.php?s=$1+$2 [QSA,L]
    RewriteRule ^morepages/([^/]+).html /index.php?s=$1 [QSA,L]

  11. Mark / t31os
    Moderator
    Posted 2 years ago #

    I really should of posted this previously, see link below for info on additional filters and methods for adding custom rewrite rules.
    http://codex.wordpress.org/Function_Reference/WP_Rewrite

    Hope that helps...

  12. sac1978
    Member
    Posted 2 years ago #

    I should have updated last night that I tried the following and its working :)

    This is what I came up with...

    add_action('generate_rewrite_rules', 'my_rewrite_rules');
    function my_rewrite_rules( $wp_rewrite )
    {
    	$new_rules = array(
    	'morepages/(.+).html' => 'index.php?s=' .
    	$wp_rewrite->preg_index(1) );
    	$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
    }

    I then added RewriteRule ^morepages/([^/]+).html /index.php?s=$1 [QSA,L] to my .htaccess file and it works.

    I personally did not think I needed the .htaccess bit once it was in wordpress. But saying that I have to add RewriteRule ^search/(.+)?$ /index.php?s=$1 [QSA,L] to get the default WP permalink search to work also.

  13. asialove
    Member
    Posted 2 years ago #

    I use one plug in to the "+" to "_"

    please note i edit the rewrite.php, and change the default search base "/search/" to "info". so edit your plugin code to any your own search base used.

    demo: http://verizon-mobile.org/info/usb_drivers

    <?php /*
    Plugin Name: Nice Search
    Version: 0.2
    Plugin URI: http://txfx.net/code/wordpress/nice-search/
    Description: Redirects ?s=query searches to /search/query, and converts %20 to +
    Author: Mark Jaquith
    Author URI: http://txfx.net/
    */
    
    function txfx_nice_search() {
    	if ( is_search() && strpos($_SERVER['REQUEST_URI'], '/wp-admin/') === false && strpos($_SERVER['REQUEST_URI'], '/info/') === false ) {
    		wp_redirect(get_bloginfo('home') . '/info/' . str_replace(' ', '_', str_replace('%20', '_', get_query_var('s'))));
    		exit();
    	}
    }
    
    add_action('template_redirect', 'txfx_nice_search');
    
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic