搜索URL wp-重定向后重写?

时间:2012-10-28 作者:Fatih Toprak

我开始在我的“搜索结果”页面上使用此功能。

#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;
}
我还将这一行添加到.htaccess-文件

RewriteRule ^icerik/(.+)?$ /index.php?s=$1 [QSA,L]
但是,每当我在搜索字段中搜索某个内容时,URL不会重定向到新的URL,即使新链接实际上有效,我现在有两个链接。

示例:

这是重写的URL:http://www.fatihtoprak.com/icerik/linux.html

这是常规URL:http://www.fatihtoprak.com/?s=linux

我的问题是:当用户使用我网站上的搜索字段时,如何显示重写的URL?

---UPDATED---

使用此插件解决了此问题&;班here.. 我还有另一个问题。它就像;

当我键入土耳其语字符(İ、Ü、ü、Ö、ö、Ğ、ş、Ş)的单词时,会出现gost 404错误页面:(你对此有什么想法吗?请键入并搜索Atatürk ve Cumhuriyet. 此外,查询结果挂钩也不关心空格(:

2 个回复
最合适的回答,由SO网友:Serkan Algur 整理而成

法提赫,

使用str\\u replace处理土耳其字符问题。像这样更改代码;

/*
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();

SO网友:Abdussamad

wp\\U重写不会重定向。它只是将请求的URL解析为wordpress能够理解的查询字符串。

您可以使用此插件重定向:

/*
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();

结束

相关推荐