Cara mudah redirect header PHP query WordPress

Cara mudah redirect halaman blog WordPress bisa diterapkan di htaccess atau dengan function header PHP berikut.

Cara mudah melakukan redirect halaman blog WordPress bisa diterapkan di htaccess atau dengan menggunakan function redirect header PHP berikut. Pasang di file functions.php theme Anda, and save it:

// change global search rules search page, paged and feed
add_action( 'init', 'jv_change_search_base_wp' );
function jv_change_search_base_wp()
{
    global $wp_rewrite;
	
	$search_base = 'topic'; // search_base
	
	if ( $wp_rewrite->search_base != $search_base )
	{
		$wp_rewrite->search_base      = $search_base;
		$wp_rewrite->search_structure = $wp_rewrite->search_base . '/%search%';
	}
	
    return $wp_rewrite;
}

// adding a new rule for empty search query
add_filter( 'search_rewrite_rules', 'jv_additional_search_rewrite_rules', 10, 1 );
function jv_additional_search_rewrite_rules( $rules )
{
	global $wp_rewrite;
	
	$new_rules = array(
        user_trailingslashit( $wp_rewrite->search_base ) . '?$' => 'index.php?s=' . $wp_rewrite->preg_index(1),
    );
	
    $rules = $rules + $new_rules;
	
    return $rules;
}

// flush_rules() if our rules are not yet included
add_action( 'wp_loaded', 'jv_flush_rules' );
function jv_flush_rules()
{
	global $wp_rewrite;
	
	$rules       = get_option( 'rewrite_rules' );
	$search_base = 'topic';  // search_base
	if ( ! isset( $rules[ $search_base . '/(.+)/?$'] ) )
	   	$wp_rewrite->flush_rules();
}

// redirect ?s= to search base
add_action( 'template_redirect', 'jv_template_redirect' );
function jv_template_redirect()
{
	global $wp_rewrite;
	
	if ( is_search() && isset ( $_GET['s'] )  )
	{
		$s         = sanitize_text_field( $_GET['s'] ); // or get_query_var( 's' )
		$location  = '/';
		$location .= trailingslashit( $wp_rewrite->search_base );
		$location .= ( ! empty ( $s ) ) ? user_trailingslashit( urlencode( $s ) ) : '';
		$location  = home_url( $location );
		wp_safe_redirect( $location, 301 );
		exit;
	}
}

Kode ini hanya untuk me-redirect url search default, dan khusus function wp_redirect bisa Anda terapkan pada berbagai parameter query atau redirect halaman tertentu. Semoga bermanfaat.

Update 30-03-2016
Fix flush rules
Tidak direkomendasikan untuk mengutak-atik core file WordPress

Author: Jevuska

Rachmanuddin Chair Yahya a.k.a Jevuska is the founder of Jevuska.Com, a qualified web about offering medical articles, blogging, tips, and tutorial of WordPress. Having written for Jevuska since 2007.

21 thoughts on “Cara mudah redirect header PHP query WordPress”

  1. sebelumnya makasih mas jevuska atas ilmu2 yang luar biasa di site ini.. :D

    saya mw bertanya kalo pengen membuat url search jadi spt ini :

    example.com/ke/keyword-pencarian.html

    gmn yah mas ??

    terima kasih :)

  2. Mas Jev,

    Setelah saya tambahin script itu di function.php kok malah jadi gini ya kalo di search manual(di kolom search)

    domainku.com/search/.htmlkeyword+keyword+keyword.html

    1. Itu ekstensi .html nya dari mana mas? Script ini khusus yang tidak pakai ekstensi karena ini untuk url search default WP.

      1. Kalo sewaktu dulu masih edit di rewrite.php, saya tambahin %search%.html default nya kan %search%.

        Trs di function.php yang skrng saya tambahin $wp_rewrite->search_structure = $wp_rewrite->search_base . ‘/%search%.html’;
        Kalo di search manual di kolom search malah keluarnya kayak gitu. keluar simbol “+” juga

        Ini udah terlanjur ke index dengan permalink /%postname%.html. Solusi nya gmn mas?

        1. Ini sama juga mas seperti itu. Pastinya tidak direkomendasikan lagi mengutak-atik file core WordPress. Update ke versi yang terbaru. Untuk menambahkan extensi.
          Ubah function jv_change_search_base_wp

          function jv_change_search_base_wp()
          {
              global $wp_rewrite;
              $search_base = 'topic'; // search_base
               
              if ( $wp_rewrite->search_base != $search_base )
              {
                  $wp_rewrite->search_base      = $search_base;
                  $wp_rewrite->search_structure = $wp_rewrite->search_base . '/%search%.html';
              }
              return $wp_rewrite;
          }
          

          dan function jv_template_redirect

          function jv_template_redirect()
          {
              if ( is_search() && isset ( $_GET['s'] ) )
              {
          		global $wp_rewrite;
                  $s         = sanitize_text_field( $_REQUEST['s'] ); // or get_query_var( 's' )
                  $location  = '/';
                  $location .= trailingslashit( $wp_rewrite->search_base );
                  $location .= ( ! empty ( $s ) ) ? urlencode( $s ) . '.html' : '';
          		$location  = home_url( $location );
          		
                  wp_safe_redirect( $location, 301 );
                  exit;
              }
          }
          
          1. Udah berhasil mas Jev, tapi kok masih keluar tanda ‘+’ ya?

            ex : domain.com/search/keyword+keyword+keyword.html

            1. Iya mas, itu memang untuk struktur url yang pakai tanda plus. Jika menggunakan tanda minus, agak mirip dengan cara me-redirect halaman 404, ada tambahan 2 function jv_parse_request dan jv_remove_char, dan kita ubah encode search query di function jv_template_redirect. Jadinya seperti ini:

              function jv_template_redirect()
              {
                  if ( is_search() && isset ( $_GET['s'] ) )
                  {
              		global $wp_rewrite;
                      $s         = sanitize_text_field( $_REQUEST['s'] ); // or get_query_var( 's' )
                      $location  = '/';
                      $location .= trailingslashit( $wp_rewrite->search_base );
                      $location .= ( ! empty ( $s ) ) ? sanitize_title_with_dashes( $s ) . '.html' : ''; // sanitize search query with dashes
              		$location  = home_url( $location );
              		
                      wp_safe_redirect( $location, 301 );
                      exit;
                  }
              }
              
              add_action( 'parse_request', 'jv_parse_request' );
              function jv_parse_request( $query )
              {
              	if ( ! is_admin() && isset( $query->query_vars['s'] ) )
              	{
              		$s = jv_remove_char( $query->query_vars['s'] );
              		$query->set_query_var( 's',  $s );
              	}
              }
              
              function jv_remove_char( $q = null )
              {
                  $q   = sanitize_title_with_dashes( urldecode( $q ), '', 'save' );
                  $q   = wp_strip_all_tags( $q );
                  $q   = preg_replace( '/&#?[a-z0-9]+;/i','', $q );
                  $q   = preg_replace( '/[^%A-Za-z0-9 _-]/', ' ', $q );
                  $q   = preg_replace( '/&.+?;/', '', $q );
                  $q   = preg_replace( '/_+/', ' ', $q );
                  $q   = preg_replace( '/\s+/', ' ', $q );
                  $q   = preg_replace( '|-+|', ' ', $q );
                  $q   = urlencode( trim( $q ) );
                  return $q;
              }
              

Leave a Reply

Your email address will not be published. Required fields are marked *

Use tag [php] to add code, e.g. [php]<?php echo $var; ?>[/php]