Running a WooCommerce site and I can successfully change the search slug from ?s=xxx to /search/xxx using one of the following:
htaccess:
RewriteCond %{QUERY_STRING} \\?s=([^&]+) [NC]
RewriteRule ^$ /search/%1/? [NC,R,L]
functions.php
function wp_change_search_url() {
if ( is_search() && ! empty( $_GET['s'] ) ) {
wp_redirect( home_url( "/search/" ) . urlencode( get_query_var( 's' ) ) );
exit();
}
}
add_action( 'template_redirect', 'wp_change_search_url' );
The problem occurs when i change the term "search" to something else. In my case swedish: /sok/. Then the searchfunction stops working and Worpdress says "Nothing found". I tried it on 3 servers, 3 themes, 0 plugins. Same result. If "search" is there it works fine, any other word it stops.
Any idea why?
Edit: The slug "find" also works. I've read all over the internet that people are having problems with this - No solutions seems to have been found though.
You'll also need to change rewrite rules and flush permalinks after, so the process to change search
slug to sok
would be as follow:
Fist you start by creating proper redirect from the default search link to your targeted URL
function change_sok_url() {
if ( is_search() && ! empty( $_GET['s'] ) ) {
wp_redirect( home_url( "/sok/" ) . urlencode( get_query_var( 's' ) ) );
exit();
}
}
add_action( 'template_redirect', 'change_sok_url' );
Then change rewrite rules to match the new slug
function rewrite_sok_slug() {
add_rewrite_rule(
'sok(/([^/]+))?(/([^/]+))?(/([^/]+))?/?',
'index.php?s=$matches[2]&paged=$matches[6]',
'top'
);
}
add_action( 'init', 'rewrite_sok_slug' );
Then flush permalinks by saving permalink settings from http://yoursite.com/wp-admin/options-permalink.php , just click Save Changes, this should flush rewrite rules and apply the new structure.
Note 1: This is tested and working.
Note 2: No need for .htaccess
changes.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.