panjianom
panjianom

Reputation: 236

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.

Upvotes: 0

Views: 843

Answers (2)

panjianom
panjianom

Reputation: 236

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');

Upvotes: 0

anubhava
anubhava

Reputation: 784898

You can insert this code just below you 301 rule:

RewriteRule "^(search)/([^ +]*)[ +]+([^ +]*[ +].*)$" /$1/$2-$3 [L,NE]
RewriteRule "^(search)/([^ +]*)[ +]([^ +]*)$" /$1/$2-$3 [L,R=302,NE]

Upvotes: 2

Related Questions