Custom Search URL: Best way to create custom search URL

The search form is an essential and important feature for any website, especially for blog type website. Sometimes, you may need to change the search URL and need to create custom search URL.

Custom search URL makes your website more professional and provides a better user experience to the visitors. Then the search URL looks clean and smart.

Benefits of using a Search form

A search form is more beneficial to provide a smart way to your visitors to find out the expected result. It helps the visitors to find their expected service or content easily.

Without using a search form, you can’t provide a better way for the visitors to find your services. So, you can’t skip it at all.

When needing a custom search URL

Usually, when an user searches something in the search form, the search result is passed through a form with an action. Usually, the name of the input field where users write their search query is “s”. I mean search query is processed by the input field’s name “s”. If you don’t know how the WordPress search form work, you can check out this page.

As a result, the default search URL becomes like this-

Default Search URL

But, it is not looking clear or smart or nice at all. So, to make it clean and smarter, you need to rewrite the search URL. If you rewrite search URL, it will look nice and smart.

So, if want to clean up your search URL, just use this trick. Otherwise, you can skip it.

How to Create a custom search URL?

Rewriting the search URL is a very easy and simple task. You just need to pass a redirect function through template_redirect hook. However, don’t get boring, just add the following codes in the functions.php file-

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

Now the search URL will look like below-

Custom Search URL

Here, search word is used instead of s and now a clean and smart search URL is showing. If you need to change the search word also and want to put any word like result or something else, you have to add some extra code in functions.php. The code snippet for this is below-

<?php 
function wpq_search_rule(){
    add_rewrite_rule('^result/([^/]*)?', 'index.php?s=$matches[1]', 'top');
}
add_action( 'init', 'wpq_search_rule' );

Example: Suppose, I want to change the search word and replace it to result, then I have to add the following code snippet in my functions.php file.

Code Snippets 1:

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

Code Snippets 2:

<?php 
function wpq_search_rule(){
    add_rewrite_rule('^result/([^/]*)?', 'index.php?s=$matches[1]', 'top');
}
add_action( 'init', 'wpq_search_rule' );

Finally, I will get the custom search URL like this-

Custom Search URL with Custom Word

Leave a Reply

Your email address will not be published. Required fields are marked *

I am available for freelance work

Let's start a project with the best services and full satisfaction guaranteed. Clean and standard coding. Best service is the goal of this project.

Hire Me

Message Me