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.

I started using this functions for my "search results"-page.

#SearchRewrite
add_action('generate_rewrite_rules', 'my_rewrite_rules');
function my_rewrite_rules( $wp_rewrite )
{
    //RewriteRule ^icerik/(.+)?$ /index.php?s=$1 [QSA,L]
    $new_rules = array(
    'icerik/(.+).html' => 'index.php?s=' .
    $wp_rewrite->preg_index(1) );
    $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}

Also I added this line to my .htaccess-file

RewriteRule ^icerik/(.+)?$ /index.php?s=$1 [QSA,L]

But, whenever I search for something in my search-field the URL doesnt redirect to new URL—even though the new links actually works, I now have two links.

Example:

This is the rewritten URL: http://www.fatihtoprak.com/icerik/linux.html

This is the regular URL: http://www.fatihtoprak.com/?s=linux

My question is then: how do I make the rewritten URL display, when a user uses the search-field on my website?

---UPDATED---

It solved with this Plugin & Class here.. Also i have another question about this. Its like ;

when i type a word with Turkish character (İ,Ü,ü,Ö,ö,ğ,Ğ,ş,Ş) it gost 404 Error page :( Do you have any idea for this ? Please type and search Atatürk ve Cumhuriyet. Also the query results hook doesn't care with spaces too (:

share|improve this question
    
Thank you so much @Frederick Andersen for your grammer offers. Sorry about my bad English :/ –  Fatih Toprak Oct 28 '12 at 12:17
add comment

2 Answers

up vote 2 down vote accepted

Fatih,

Use str_replace for Turkish chars issue. Change code like this;

/*
Plugin Name: Redirect Search Results
Author: Abdussamad
Plugin URI: http://wordpress.stackexchange.com/questions/70740/search-url-wp-rewrite-after-redirect
*/
class redirect_search_results {
function __construct() {
    add_action( 'init', array( $this, 'redirect' ) );
}

function redirect() {

    if( isset( $_GET[ 's' ] ) ) {
            $turkish= array("İ","ı","Ü","ü","Ö","ö","ğ","Ğ","ş","Ş");
            $nturkish = array("i","i","U","u","O","o","g","Ğ","s","S");
            $quest = str_replace($turkish,$nturkish,$_GET['s']);
        wp_redirect( home_url( '/icerik/' . $quest . '.html' ), 301 );
        exit;
    }
}
}

new redirect_search_results();
share|improve this answer
    
Thank you so much Serkan, but got another thing is, search_query on my search.php title. I use this method <strong><?php echo get_search_query(); ?> on my search results page title. This title should be include unconverted the characters ? Is that possible ? Must be like Atatürk (: –  Fatih Toprak Oct 29 '12 at 16:43
    
Fatih, WordPress uses get method for search forms. Use echo $_GET['s'];. I think this code works good :) –  Serkan Algur Oct 29 '12 at 16:47
    
Also you can use <?php the_search_query(); ?> –  Serkan Algur Oct 29 '12 at 16:55
    
i just try <?php the_search_query(); ?> it didint display anything. –  Fatih Toprak Oct 29 '12 at 17:11
add comment

wp_rewrite doesn't redirect. It simply parses requested URLs into query strings that wordpress understands.

You can redirect using this plugin:

/*
Plugin Name: Redirect Search Results
Author: Abdussamad
Plugin URI: http://wordpress.stackexchange.com/questions/70740/search-url-wp-rewrite-after-redirect
*/
class redirect_search_results {
function __construct() {
    add_action( 'init', array( $this, 'redirect' ) );
}

function redirect() {
    if( isset( $_GET[ 's' ] ) ) {
        wp_redirect( home_url( '/icerik/' . $_GET[ 's' ] . '.html' ), 301 );
        exit;
    }
}
}

new redirect_search_results();
share|improve this answer
    
So, Whats your ideas Abdussamad ? –  Fatih Toprak Oct 28 '12 at 23:12
1  
I have edited my answer with a redirection plugin. –  Abdussamad Oct 29 '12 at 11:18
    
I appreciate you. Thans for your help. –  Fatih Toprak Oct 29 '12 at 15:31
    
Also i got another problem it goes to new url but when i type a word with Turkish character (İ,Ü,ü,Ö,ö,ğ,Ğ,ş,Ş) it gost 404 Error page :( Do you have any idea for this ? Please type and search Atatürk ve Cumhuriyet. This is the results page –  Fatih Toprak Oct 29 '12 at 15:49
    
Atatürk redirects properly but some other words like için don't. The problem is that WordPress runs URLs through the sanitize_title_with_dashes function and that removes special characters. That is why searches are treated as query strings and not pretty URLs. So there is no solution that I can think of where you can have pretty URLs for searches as well as special characters in search keywords. –  Abdussamad Oct 30 '12 at 7:01
add comment

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.