1

I have rewritten the search_base to suche (german) to get a url like this: http://mypage.com/suche/searchterm

Now when I submit the search form I get to the url /?s=searchterm.

The internetz gives me the same workaround from multiple sources, such as this one: Search results URL without query string variables

add_action("template_redirect", function() {
    if (is_search() && !empty( $_GET["s"] ) ) {
        if (wp_redirect( home_url('/suche/') . urlencode(get_query_var('s')) )) {
            exit();
        }
    }
});

My problem with that solution:

  • the redirect results in a noticeably longer delay (even in my local environment)
  • I have to hardcode the suche again, whereas I would like to use the altered search-base

I tried creating my own custom search form with add_filter("get_search_form", ... with action="http://mypage.com/suche/ which still resulted in a get var /suche/?s=searchterm which obviously didn't work.

How can I tell the search form to go directly to /suche/searchterm

Thanks!

  • Use JavaScript to avoid the redirect, or mod_rewrite (also use it as a fallback). The delay stems from having to load WP for the redirect - if you used plain mod_rewrite, it would be much faster. – janh Nov 14 '17 at 9:33
  • I don't understand how a mod_rewrite can alter the url the search form sends to– Isn't the point behind mod_rewrite to leave urls as is and interpret stuff under the hood? – there must be a way to hook into the search send, where the url is generated, mustn't it? – kater louis Nov 14 '17 at 9:54
  • mod_rewrite can do normal HTTP redirects (just use an absolute URL as the "to" part of the rule, or use the R flag), it just doesn't need to load WP (which is pricey) for that. The URL is generated by the browser, so WP (server-side) cannot influence it. You will have to use JavaScript if you don't want any redirects to happen and the request to go to the correct pretty URL directly. – janh Nov 14 '17 at 10:06
  • Could you please provide en example of your suggested solution in an answer post? – kater louis Nov 14 '17 at 10:20
0

Assuming you make your form's action point to the correct URL, here are Two (and a half) Options:

With Javascript (jQuery)

jQuery(document).ready( function() {
    jQuery("#searchform").submit( function() {
        self.location.href = jQuery(this).attr("action") + jQuery("#s").val();
        return false;
    });
});

You could also do it without jQuery (if you don't use jQuery on your site and don't want to load it just for this). Here's a simple version of that (probably not super cross browser, but I haven't tested it), which you can expand on:

<form action="/suche/" onsubmit="self.location.href=this.getAttribute('action') + document.getElementById('s').value; return false;">
<input type="text" name="s" id="s" />
<input type="submit"></form>

With a rewrite rule:

RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^s=(.*)
RewriteRule ^(.*?)/?$ $1/%1? [R=302,L]

This would work on any URL that gets ?s=... passed, you might want to limit that to your actual search, as in

RewriteEngine On
RewriteCond %{QUERY_STRING} ^s=(.*)
RewriteRule ^suche/$ /suche/%1? [R=302,L]

And if you pass multiple arguments in that form (say, a category and a string to search for), this would have to be adapted accordingly, right now it just assumes that there's only one parameter, and it's the query string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.