使用 - 而不是WordPress中的+搜索永久链接

时间:2010-05-26 03:17:40

标签: wordpress .htaccess mod-rewrite permalinks

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]+ /(#[^?& ]*)??([^& ]*&)?s=([^& ]+)[^ ]* HTTP/ 
RewriteRule ^$ http://wordpressblog.com/search/%3? [R=301,L] 

目前我使用上面的.htaccess mod_rewrite规则来转换默认的WordPress搜索永久链接:

http://wordpressblog.com/?s=key+word

这样的好永久链接:

http://wordpressblog.com/search/key+word

我的问题是:上面的mod_rewrite规则的哪一部分我需要更改以获得更好的永久链接,如下所示:

http://wordpressblog.com/search/key-word.html

感谢。

2 个答案:

答案 0 :(得分:1)

这对我有用。启用固定链接后,搜索无效。

在wp_head()之后将此JQUERY SCRIPT添加到您的THEME header.php文件中;标签

要使其正常工作,您还必须通过在header.php中添加<?php wp_enqueue_script('jquery'); ?>来启用jquery BEFORE wp_head();标签

示例:

<?php wp_enqueue_script('jquery'); ?>
<?php
    /* We add some JavaScript to pages with the comment form
     * to support sites with threaded comments (when in use).
     */
    if ( is_singular() && get_option( 'thread_comments' ) )
        wp_enqueue_script( 'comment-reply' );

    /* Always have wp_head() just before the closing </head>
     * tag of your theme, or you will break many plugins, which
     * generally use this hook to add elements to <head> such
     * as styles, scripts, and meta tags.
     */
    wp_head();

?>
<script type="text/javascript"> 
    jQuery(document).ready(function() {
        jQuery("#searchform").live('submit',function(){

            location.href='/search/' + encodeURIComponent(jQuery("#s").val()).replace(/%20/g, '+'); return false;       

        }); 
    }); 
</script> 

答案 1 :(得分:0)

如果我正确的话,当你重定向时;

?s=hello+world

到此;

/search/hello-world.html

WordPress实际上会搜索'hello-world.html',我怀疑你会得到任何结果(假设'hello + world',其中加号是URL解码为实际'空格',确实会返回结果)。

因此,在进行搜索之前,您还需要插入WordPress,以便将搜索字段清理回原来的状态。

另外似乎是a pain to do character replacement in Apache rewrites - 您必须为每个“加号”事件编写规则。

如果我是你,我会使用PHP在WordPress内部做所有事情。如果你喜欢这种声音,我可以发布解决方案吗?