1

I have a search.php file in my theme that displays search results.

Search queries directed to https://example.com/search/query/ display the search results correctly. WordPress natively supports this URL structure.

I would like to be able to browse directly to https://example.com/search/ without adding a search query, and have it display my search.php file, but it returns a 404 error.

How can I make this page work?

5
  • https://example.com/search/query/ is actual https://example.com?s=query which is generated by wordpress permalink structure.
    – GKS
    Nov 24, 2016 at 16:09
  • @GovindKumar Yes I know, but browsing to https://example.com/?s= does not return a 404 error, while https://example.com/search/ does.
    – Swen
    Nov 24, 2016 at 16:15
  • 1
    Answering this question requires knowledge of how that plugin works.
    – Milo
    Nov 24, 2016 at 16:17
  • Actually, WordPress natively supports the https://example.com/search/query/ structure. I have updated my question.
    – Swen
    Nov 24, 2016 at 16:54
  • ok, I edited the question a bit for clarity, since plugin is not involved at all.
    – Milo
    Nov 24, 2016 at 17:45

1 Answer 1

2

The search rewrite rule only captures the pattern search/(.+)/?$. A request to search/ matches the root pages rule, so WordPress tries to find a page with that slug. You could create a page at that location and load a template via a filter, or, you could add a rewrite rule that just does an empty search:

function wpd_empty_search_fix(){
    add_rewrite_rule(
        '^search/$',
        'index.php?s=',
        'top'
    );
}
add_action( 'init', 'wpd_empty_search_fix' );
2
  • That rewrite rule did not work for me. I guess I'll try creating the search page manually.
    – Swen
    Nov 25, 2016 at 16:35
  • @Swen - visit the Settings > Permalinks page to flush rewrite rules. New rules aren't picked up until the option that stores them long-term is emptied and repopulated.
    – Milo
    Nov 25, 2016 at 18:52

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.