0

when typing "a" into my search field (/?s=a) I my search.php template is loaded and all results that match that letter are shown.

If I add this to my .htacess …

RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{QUERY_STRING} ^s=(.+)$
RewriteRule .* /searchmyblog/%1/? [R,L]

and then type "a" into my search the first result of my search is shown. So there is no search.php template shown with all the results but rather the page is redirected to "mypage.com/somecategory/amore-mio"

Why is that happening? I simply want to rewrite the normal /?s= to /search/ or any other custom url.

Any ideas on that? I found heaps of posts online, but none of them seem to be state of the art or use some weird JS-hacks that I find unnecessary.

Ideas on that?

1
  • This seems more like an Apache mod_rewrite question rather than WordPress. Commented Aug 31, 2012 at 2:24

1 Answer 1

3

WordPress doesn’t know that you want the path /searchmyblog/ as search result base. So when it sees the a it tries to find the best match – in your case a post starting with this letter.

To fix that you could modify my code for the pagination base to change the search base:

if ( ! function_exists( 't5_search_base' ) )
{
    register_activation_hook(   __FILE__ , 't5_flush_rewrite_on_init' );
    register_deactivation_hook( __FILE__ , 't5_flush_rewrite_on_init' );
    add_action( 'init', 't5_search_base' );

    function t5_search_base()
    {
        $GLOBALS['wp_rewrite']->search_base = 'searchmyblog';
    }

    function t5_flush_rewrite_on_init()
    {
        add_action( 'init', 'flush_rewrite_rules', 11 );
    }
}

Now your .htaccess should work.

7
  • Thank you, but that doesn't work for me… If I add your lines to my functions.php (and keep my proposed .htaccess lines) and type into my search field I get no results at all. Instead the url in my address-bar changes to ….com/?search-box=test. The weird thing is that search-box is the name attribute of my search-inputfield. Commented Aug 31, 2012 at 6:10
  • The name attribute must be s in WordPress.
    – fuxia
    Commented Aug 31, 2012 at 6:43
  • Thank you for the quick response. I changed the name attribute to s … Now the same happens as previously described. When typing a into the search field the first match is shown directly - no search-results. When typing kdfa or something weird the addressbar changes to .com/searchmyblog/kdfa but results in a 404. So the 404 page is loaded. Commented Aug 31, 2012 at 6:56
  • Did you refresh the permalink settings? In your functions.php this cannot happen automatically (and it really doesn’t belong there).
    – fuxia
    Commented Aug 31, 2012 at 7:03
  • 2
    Thank you, I found the solution with your help and your posts. I had flush_rewrite_rules() in one of my custom-post-templates that might have screwed things up. Commented Aug 31, 2012 at 10:20

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.