[wp-hackers] Enabling pretty url search redirect

Alex King lists at alexking.org
Wed Nov 21 16:22:55 UTC 2012


You should do your redirect way earlier in the stack (init, wp_loaded, etc.) so that WordPress doesn't do all the DB queries, etc. that it needs to pull the content to display. I'd experiment to find the earliest place this could be done.

You might also want to consider doing it at the .htaccess/mod_rewrite level so that it doesn't hit the PHP/WordPress layer at all.

Cheers,
--Alex

http://alexking.org | http://crowdfavorite.com




On Nov 21, 2012, at 9:07 AM, Tom Barrett <tcbarrett at gmail.com> wrote:

> I want to redirect all front end searches (?s=) onto the /search/ pretty
> permalink. For pages (?p=) this happens automatically when permalinks are
> switched on, but search is different.
> 
> If that isn't clear, here are two links. The first redirects to the pretty
> url, the second doesn't:
> http://www.tcbarrett.com/?p=1038
> http://www.tcbarrett.com/?s=donate
> 
> I'm currently using a hook:
> add_action( 'template_redirect', 'tcb_search_pretty_permalinks' );
> function tcb_search_pretty_permalinks(){
>  if( !is_admin() and is_search() ):
>    if( $search = $_GET['s'] ):
>      wp_redirect( home_url("search/{$search}/") );
>      exit;
>    endif;
>  endif;
> }
> 
> But wondered if there was a better way? I'm a little worried that I've
> missed something and am now breaking things...
> 
> -- 
> http://www.tcbarrett.com | http://gplus.to/tcbarrett |
> http://twitter.com/tcbarrett



More information about the wp-hackers mailing list