Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
    
This seems more like an Apache mod_rewrite question rather than WordPress. –  Joseph Aug 31 '12 at 2:24
add comment

1 Answer

up vote 2 down vote accepted

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.

share|improve this answer
    
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. –  mathiregister Aug 31 '12 at 6:10
    
The name attribute must be s in WordPress. –  toscho Aug 31 '12 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. –  mathiregister Aug 31 '12 at 6:56
    
Did you refresh the permalink settings? In your functions.php this cannot happen automatically (and it really doesn’t belong there). –  toscho Aug 31 '12 at 7:03
1  
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. –  mathiregister Aug 31 '12 at 10:20
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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