Search code examples
wordpress.htaccesspermalinks

Replace plus (+) with dash (-) in Wordpress search


How can i change the url plus(+) sign to dash(-) in url with htaccess. This is the code i have so far:

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

This works well and the result something like this: http://example.com/search/key+word.html

The only thing is that i want to change plus with dash. I would really appreciated if anyone can help out.


Solution

  • I decide to make it possible by using plugin with this code:

    function seo_search_result() {
        if ( is_search() && strpos($_SERVER['REQUEST_URI'], '/wp-admin/') === false && strpos($_SERVER['REQUEST_URI'], '/search/') === false ) {
            wp_redirect(get_bloginfo('home') . '/search/' . str_replace(' ', '-', str_replace('%20', '-', get_query_var('s'). '.html')));
            exit();
        }
    }
    
    add_action('template_redirect', 'seo_search_result');