开发者

Using - Instead of + in WordPress Search Permalink

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]+ /(#[^?& ]*)??([^& ]*&)?s=([^& ]+)[^ ]* HTTP/ 
RewriteRule ^$ http://wor开发者_如何学Godpressblog.com/search/%3? [R=301,L] 

Currently I use the above .htaccess mod_rewrite rule to convert default WordPress search permalink:

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

into nice permalink like this:

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

My question is: What part of the mod_rewrite rule above that I need to change to get a nicer permalink like this one:

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

Thanks.


This worked for me. Search wasn't working when I had permalinks enabled.

Add this JQUERY SCRIPT into your THEME header.php file AFTER wp_head(); tag.

For this to work, you must also have jquery enabled by adding <?php wp_enqueue_script('jquery'); ?> in the header.php BEFORE wp_head(); tag.

Example:

<?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> 


If I'm thinking right, when you redirect this;

?s=hello+world

to this;

/search/hello-world.html

WordPress will actually search for 'hello-world.html', which I doubt you'll get any results for (presuming 'hello+world', where the plus is URL decoded to an actual 'space', does return results).

So you'd also need to plug into WordPress before it makes the search, in order to sanitize the search term back to what it was.

Plus it seems a pain to do character replacement in Apache rewrites - you'd have to write a rule for each number of 'plus' occurrences.

If I were you, I'd do everything inside WordPress itself, using PHP. I could post a solution if you like the sound of that?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜