Use custom URL for default search results page

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Use custom URL for default search results page

This topic contains 12 replies, has 2 voices, and was last updated by Ernest Marcinko Ernest Marcinko 1 year, 4 months ago.

Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #39968
    thebizpixie
    thebizpixie
    Participant

    Hi Ernest,

    I’m looking for a way to set a custom URL for the WordPress search results page, and still have the Ajax Search Pro query determine the order of content.

    I found a way to set a custom URL in WordPress using:

    	if ( is_search() && !empty( $_GET['s'] ) ) {
    		wp_redirect( home_url( "/search/" ) . urlencode( get_query_var( 's' ) ) );

    which makes the search results page /search/{phrase} instead of ?s={phrase} e.g. /search/elephant instead of /?s=elephant

    but this appears to redirect the page after the Ajax Search Pro query has been run, and the results that come from Ajax Search Pro are not being passed to the new search results page location. Instead, if I use this redirect, I’m getting the default WordPress search results.

    Do you know of a way that I can set a custom URL for the WordPress search results page, and still be able to override the default WordPress search results with the results from my Ajax Search Pro instance?

    I have tried adding /search/{phrase} to the Custom redirect URL, but because I’m redirecting to the default WordPress results page on enter or click and not to the custom URL, it doesn’t have any effect.

    I would rather modify the default WordPress search URL instead of just adding a pseudo-search results page at /search/, to ensure that all WordPress searches go through the same core search page/template, not just the ones that come through an Ajax Search Pro form.

    Can you see the best way to make this work seamlessly?

    Thanks your help with this,

    Nikki

    #39969
    thebizpixie
    thebizpixie
    Participant

    I think the solution might look something like this, but I’m just not sure on the right way to approach it:
    https://wp-dreams.com/forums/topic/custom-results-page-with-elementor/

    #39978
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi,

    This article will help you with that.
    I recommend using the .htaccess mod_rewrite rule solution, usually it is faster and safer. This basically keeps your original search page, but changes the URL to however you need it.

    You may need to google a bit about htaccess and mod_rewrite rules on how to exactly do them, it is not very simple unfortunately. I am no expert in apache mod_rewrite, and it is also out of the bounds of this support, but I hope I helped you to the right direction.

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #40040
    thebizpixie
    thebizpixie
    Participant

    I’m comfortable with .htacess rules, and the redirection rule worked perfectly using htaccess instead of the function.

    But the results on the search page still revert to the default WordPress results, and not the Ajax Search Pro query.

    Any other suggestions as to how I can get the Ajax Search Pro query passing through to the redirected page and overriding the WordPress results?

    • This reply was modified 1 year, 5 months ago by thebizpixie . Reason: Didn't test it properly before replying
    • This reply was modified 1 year, 5 months ago by thebizpixie .
    #40065
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Try enabling the soft check feature here. If the results page uses a custom query, then this may help with that.

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #40077
    thebizpixie
    thebizpixie
    Participant

    Yeah, I tried that already, so all of this testing has been with the soft check feature on, and still the override doesn’t work on the modified URL.

    If there’s nothing else I can try, I think I may have to accept that I can’t modify the default search results page URL and still have Ajax Search Pro work correctly.

    If I have to choose between modifying the URL and using Ajax Search Pro, then I choose Ajax Search Pro, but if you do find a solution at any point, I’d love to know about it.

    #40083
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    If the results override actually works on the default results page, then there is still hope.

    See if the .htaccess redirection works correctly, it should tunnel all of the query arguments to the orinal page. My only guess is, that maybe some of the arguments are stripped off via the .htaccess rule, so it does not trigger the override. If you are using this rule from the article:

    # Change WordPress search URL
    RewriteCond %{QUERY_STRING} \\?s=([^&]+) [NC]
    RewriteRule ^$ /search/%1/? [NC,R,L]

    ..the second line is very suspicious to me. The regular expression says, that ‘get every character after the “?s=” string, which is not the & character’ – and that basically removes any other parameter after the phrase (if I am not mistaken).

    Can you please try this variation instead:

    # Change WordPress search URL
    RewriteCond %{QUERY_STRING} \\?s=([*]+) [NC]
    RewriteRule ^$ /search/%1/? [NC,R,L]

    ..or maybe even this:

    # Change WordPress search URL
    RewriteCond %{QUERY_STRING} \\?s=(*) [NC]
    RewriteRule ^$ /search/%1/? [NC,R,L]
    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #40189
    thebizpixie
    thebizpixie
    Participant

    You’re absolutely right that the redirect rule was not correct, and I should have checked it more closely. I also forgot that GET was required to make this all work as desired.

    I have rewritten the rewrite rule to behave correctly, I believe:

    RewriteCond %{QUERY_STRING} \\?s=([^&]+)\&(.*) [NC]
    RewriteRule ^$ /search/%1/?%2 [NC,L,R]

    such that the original search URL:

    https://mydomain.com/?s=frog&asp_active=1&p_asid=1&p_asp_data=1&filters_initial=1&filters_changed=0&qtranslate_lang=0&current_page_id=36192

    now redirects to the desired URL with the parameters attached:

    https://mydomain.com/search/frog/?asp_active=1&p_asid=1&p_asp_data=1&filters_initial=1&filters_changed=0&qtranslate_lang=0&current_page_id=36192

    but unfortunately the results on the redirected page are still not coming from Ajax Search Pro, but are instead the default WordPress results.

    Another piece of the puzzle is that when I filter down my results on the redirected page using the settings box (and my custom field “content type”), the URL reverts to the default WordPress URL, but still does not override the results.

    i.e. The URL changes to:
    https://mydomain.com/?s=frog&asp_active=1&p_asid=1&p_asp_data=1&aspf%5Bcontent_type__1%5D%5B3%5D=Video&filters_initial=0&filters_changed=1&qtranslate_lang=0&current_page_id=36861

    I don’t fully understand why this happens, but it might be a clue to part of what is going on.

    Can you see any issues with this, or think of anything else that I could explore here?

    Thanks for your input.

    #40259
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    I honestly don’t know – it seems correct to me. Something else is at play here for sure, simple redirects should not affect the outcome. I have a feeling something gets lost along the way.
    You could compare the query parameters on both pages, by doing a var_dump($_REQUEST, $GET); to see if there is any difference – they should have the same data if everything redirects correctly.

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #40382
    thebizpixie
    thebizpixie
    Participant

    OK, so I’ve tested the var_dump and there is a difference.

    Without redirect
    URL is /?s=frog&asp_active=1&p_asid=1&p_asp_data=1&filters_initial=1&filters_changed=0&qtranslate_lang=0&current_page_id=36192

    array(8) { 
    ["s"]=> string(6) "frog" 
    ["asp_active"]=> string(1) "1" 
    ["p_asid"]=> string(1) "1" 
    ["p_asp_data"]=> string(1) "1" 
    ["filters_initial"]=> string(1) "1" 
    ["filters_changed"]=> string(1) "0" 
    ["qtranslate_lang"]=> string(1) "0" 
    ["current_page_id"]=> string(5) "36192" } NULL

    With redirect
    URL is /search/frog/?asp_active=1&p_asid=1&p_asp_data=1&filters_initial=1&filters_changed=0&qtranslate_lang=0&current_page_id=36192

    array(7) {
    ["asp_active"]=> string(1) "1" 
    ["p_asid"]=> string(1) "1" 
    ["p_asp_data"]=> string(1) "1" 
    ["filters_initial"]=> string(1) "1" 
    ["filters_changed"]=> string(1) "0" 
    ["qtranslate_lang"]=> string(1) "0" 
    ["current_page_id"]=> string(5) "36192" } NULL

    So there only difference is that the “s” parameter is missing from the _GET array with the redirect, but then that makes sense because we’re redirecting the URL.

    As a test, I added the ‘s parameter back to my URL, so now the redirection rule reads:

    RewriteCond %{QUERY_STRING} \\?s=([^&]+)\&(.*) [NC]
    RewriteRule ^$ /search/%1/?s=%1&%2 [NC,L,R]

    and the URL now contains the search term twice:

    /search/frog/?s=frog&asp_active=1&p_asid=1&p_asp_data=1&filters_initial=1&filters_changed=0&qtranslate_lang=0&current_page_id=36192

    and voila, it now overrides the results correctly, so that part of the problem is now resolved.

    Unfortunately, if I filter down my results using the settings, the URL still reverts to the original URL ie. /?s=frog and loses the redirected version of the URL.

    Note: Setting the Custom redirect URL to “/search/{phrase}/” does not seem to have any effect.

    Any ideas why this is happening and what I can do about it?

    Thanks for your patience and persistence with this.

    #40401
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    That is a problem, the $_GET['s'] should be set on both cases for sure, the htaccess rule should basically preset that. I don’t understand why though, that is going to be something in the rules. That is a very important query parameter, it basically tells WordPress that the current query is the search query, without that the override will not trigger. This is probably a bit more complicated as I thought. You may have to experiment a bit more with the .htaccess rules, or perhaps ask someone with a bit more experience on mod_rewrite rules.

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #40418
    thebizpixie
    thebizpixie
    Participant

    I think I’m going to leave this one for now. It’s not a critical feature to have at this point in the project, so I’m going to leave the URL as the default, and return to it at a later date if need be.

    Thanks for all of your help with this. We made great progress, and created a solid foundation for tackling this in the future.

    #40424
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    You are very welcome, let me know if you need any more help.

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


Viewing 13 posts - 1 through 13 (of 13 total)

You must be logged in to reply to this topic.