Rewriting the search URL

By default, when you are searching something on a WordPress-based website, the result link will be domain.com/?s=your-keyword. It’s always there, even you saved Permalink in Settings. So you might want to rewrite it.

For a recent customer, I also faced a similar issue which requires me to rewrite the URLs. The customer needs to have several pages with custom post types, then paginating.

Frank Bültge already wrote a post about rewriting the search URL. You can read it here. And again, because my customer needs to use custom post types, I needed to make some other rewriting rules. So combining with Frank’s code snippet, my actual code is:

 

 

Now when someone searches on their site, the search result URL is https://nhquang.com/search/corporate with corporate as the keyword. The next page is https://nhquang.com/search/corporate/page/2/ and so on. You can see it’s applied to the new rule #2 in my code. It is working well with multi languages websites. For example, I am using Polylang with English and Vietnamese.

Do you have another better option? Please share!

 

Leave a Comment

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