How to Edit the Default Search URL Slug in WordPress

Do you want to edit the default search URL slug in WordPress? Many websites change their URL slugs for a variety of reasons. The most popular reasons include making them SEO friendly and user-friendly.

By default, WordPress will not make your URLs easy to remember or SEO friendly, which is a big problem.

Your website’s SEO determines how high you will rank on search engines like Google and Bing, and there are many ways to improve your rankings. The problem is that most of the default WordPress settings are not SEO friendly and it is necessary that you change them.

Today, I will demonstrate how to edit the default search URL slug in WordPress.

What Makes the Default Search URL So Bad

It is much easier to see the difference between a good URL and a bad URL. Let’s take a look at a default search URL:[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]https://www.MyExampleWebsite.com/?s=New+Post[/ht_message]

In this example, I searched for New Post.

Now let’s look at what the other links on your website look like:[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]

https://www.MyExampleWebsite.com/a-page

https://www.MyExampleWebsite.com/category/a-category

[/ht_message]

Notice a difference?

It is very intuitive to type a URL that just includes the actual page name or the actual category name, but it is not intuitive to search for “?s=search-term” and instead, your search URL should look something like this:[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]https://www.MyExampleWebsite.com/search-term[/ht_message]

A great rule of thumb for URLs is that if they are easy to remember and help shorten the URL length, they are usually better for everything.

How to Edit the Default Search Slug in WordPress

Step 1: Locate Your Theme’s Functions File

This method will not require any additional plugins, but it will require access to your website’s cPanel. The login information is provided to you with your web hosting plans when you create an account with them.

The code used will remove the “?s=” from the search URL slug. This will make your search URL more SEO friendly.

Let’s start by logging into the cPanel and clicking on the File Manager option. The File Manager will allow you to access all of the files related to your website.

Click on the File Manager option.

You need to locate your theme’s functions file. Click on the public_html directory, then click on the wp-content folder. Inside this folder, you will find all of the content related to your website. Click on the themes folder and enter the folder of the theme you are currently using. Finally, right-click on the functions file and select the Edit option.

Select the "Edit" option.

A pop-up window will show up. This box will warn you to create a backup of your files before editing anything. This will ensure that you can revert your website back to when it was working if something goes wrong. Click on the “Edit” button. A new tab will open containing all of the code from the file.

Click on the "Edit" button.

If you are looking for a great backup service, check out the best backup services of 2020. Keeping a backup is a tremendous benefit for every website and is highly recommended.

Step 2: Editing the Functions File

Now it is time to add a few lines of code to your functions file. If you are unfamiliar with adding code, make sure you do not insert the following code within another function. It will result in an error.

Instead, copy the following lines of code and paste them at the bottom of the the file:[ht_message mstyle=”info” title=”” show_icon=”” id=”” class=”” style=”” ]function edit_search_url() {
if ( is_search() && ! empty( $_GET[‘s’] ) ) {
wp_redirect( home_url( “/search/” ) . urlencode( get_query_var( ‘s’ ) ) );
exit();
}
}
add_action( ‘template_redirect’, ‘edit_search_url’ );[/ht_message]

This code will remove the “?s=” portion of the search URL and now it will show the exact search term used instead. Once you have inserted the code into the functions file, click on the “Save Changes” button to finish.

Click on the "Save Changes" button.

You can now do a quick search on your website and see the changes to your URL. The   “?s=” will be removed and only the search term will be seen. Each word used in the search will be separated with a “+” symbol.

You can view your new Search URL on your website.

Congratulations, you have successfully made your search URL easier to read and more SEO friendly. If you want to undo this, simply delete the code you added to your functions file, which will return the URL structure back to default.

Note: If you change themes you will have to enter the code into the new theme’s Functions file.

Consider Using An SEO Plugin

SEO is a big part of every web developer’s workday and you can save quite a bit of time by using a plugin. There are a variety of WordPress plugins that help you improve your website’s SEO in a variety of ways.

One of the most popular SEO plugins is Yoast SEO. This provides tips for not just SEO, but readability. It helps you keep track of keyword usage, meta descriptions, internal links, outbound links, SEO title width, and much more.

It also provides tips to improve the readability of your content. For instance, it tells you how difficult your blog is to read. And considering the average adult ranges from 7th to 8th grade, it’s important to keep it simple.

As a result, it helps you keep the visitors your better SEO rankings bring in.

Take A Look At Your Permalink Structure

If you are looking for other ways to optimize your URL, take a look at the WordPress permalink structure you use.

WordPress has several permalink structures to choose from, however, if done correctly the differences should be negligible. Yoast SEO recommends using simple and clear permalink structures in WordPress.

It can give your website a significant boost, especially if your permalinks are still using the “?p=” style.

However, it also recommends not changing your permalink structure unless it is severely outdated or your blog is not established.  This can create a ton of work in the backend of your website, after all, you need to redirect all pages to the new structure.

Thus, make sure you take the appropriate actions if you decide to make a change.

Make All of Your URLs SEO Friendly

There are a lot of URL links for WordPress websites and one of the easiest ways to change them is to change your website’s permalink structure. This can help you change custom post types and also select the date and time format you want the URL to use.

Dates and times are included in some URLs and the format they use can be very different.

For example, in the United States, the dates are formatted Month/Day/Year, while in most European countries, the date format is Day/Month/Year. Understanding who views your links is very important because if someone used 5/7/2018, in the United States that would be May 7th, 2018, and in Europe that would be July 5th, 2018.

Did you find it easy to edit the default search URL structure? What steps have you taken to improve your website’s SEO?

2 thoughts on “How to Edit the Default Search URL Slug in WordPress”

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.