How to change the url in the wordpress site search form


Do you need to change the default search URL pool in WordPress? Standard slug urls when searching for information on the site are not the most convenient. In this tutorial, we will show you how to easily change the default search URL in WordPress and make it more optimized and user friendly.
Changing the search URL on the CNC

Why Do I Need To Change the Search Url In Wordpress

WordPress uses the SEO URL structure for all pages on your site. Typical WordPress CNC friendly URLs are as follows:
http : // example . com / some - page /
http : // example . com / 2018 / 03 / some - article /
http : // example . com / category / some - category /
If you don’t know how to install standard WordPress search on the site that we are talking about in this article, then click on the link.
As you can see from the example, these URLs are easy and understandable not only for people, but also for search engines.
And WordPress itself offers us to use this design:
http : // example . com /? s = search - term
In this form, of course, you do not need to use urls. While some WordPress search plugins improve search results, most do nothing about the search URL itself.
But what if I tell you that you can do this kind of CNC:
http : // example . com / search / your - search - term /
Let's see how to make the WordPress search page URL more SEO friendly.

Change Page Url In A Search Page In Wordpress

For this tutorial, we'll edit WordPress theme files a bit. We will do this in several ways.

Method 1. Change Wordpress Search Url Slug Through Functions.Php File

This method is simpler and recommended for most users.
First, you need to copy and paste the following code into the functions.php file of your theme or plugin for a specific site:
function wpb_change_search_url() {
    if ( is_search() && ! empty( $_GET['s'] ) ) {
        wp_redirect( home_url( "/search/" ) . urlencode( get_query_var( 's' ) ) );
        exit();
    }  
}
add_action( 'template_redirect', 'wpb_change_search_url' );
1
function wpb_change_search_url() {
    if ( is_search() && ! empty( $_GET['s'] ) ) {
        wp_redirect( home_url( "/search/" ) . urlencode( get_query_var( 's' ) ) );
        exit();
    }  
}
add_action( 'template_redirect', 'wpb_change_search_url' );
Do not forget to save the changes!
For clarity, now we go to the browser and see that the url address looks different:
http : // example . com / search / your - search - query /
Searching results

Method 2: Change the Structure of the Url Of The Site Search Url Using Htaccess

The .htaccess file is often used to configure redirection and other website configuration tasks. You can also use it to change the default search URL pool in WordPress.
Your .htaccess file is located in the root folder of your site, and you will need to use the FTP or File Manager application in cPanel to edit it. Refer to this guide if you cannot find the .htaccess file.
In your .htaccess file, you need to insert the following code below:
# Change WordPress search URL
RewriteCond %{QUERY_STRING} \\?s=([^&]+) [NC]
RewriteRule ^$ /search/%1/? [NC,R,L]
1
# Change WordPress search URL
RewriteCond %{QUERY_STRING} \\?s=([^&]+) [NC]
RewriteRule ^$ /search/%1/? [NC,R,L]
Again, do not forget to save the changes to the file and load it back into the root directory.
Now again we turn to the search on the site through the browser and the structure of the URL will look like this:
http : // example . com / search / your - search - query /
This concludes our lesson. I sincerely hope that this note helped you solve the question "how to change the URL of the search form on the site." I advise you to continue reading the article on the blog: 9 chips and hacks with the .htaccess file .
Good luck to everyone and thank you for your attention!
For more information visit our website Digital Marketing Services in UAE

Comments

  1. การดูแลผิวหน้าให้ขาวใสไร้สิวนั้นถือว่าเป็นเรื่องที่ยากมากๆ เพราะว่าบนโลกนี้มีปัจจัยหลายอย่างมากๆ ที่มีผลต่อผิวหน้าของเรา
    Ulthera เทคโนโลยียกกระชับผิวหน้า
    https://thecloverclinic.info
    .

    ReplyDelete

Post a Comment

Popular posts from this blog

The Strategies & Successes of Paid Advertising Campaign

Compromising Information in the Fight against Competitors by the Method of Black Pr Campaign