Jump to content
therebel

How to change the default search URL slug in WordPress

Recommended Posts

This short tutorial will cover the following search slug transformation:

mydomain.com/?s=search-term to mydomain.com/search/search-term

Two methods I know:

1st method: Editing Your Theme Functions File by adding at the end of the file

/**
* Change search page slug to /search/
*/

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


2nd method: Using .htaccess (apache)

# Change WordPress search URL slug to /search/
RewriteCond %{QUERY_STRING} \\?s=([^&]+) [NC]
RewriteRule ^$ /search/%1/? [NC,R,L]


Both methods presume you want to use /search/ , but you can change it to whatever you want.

Enjoy!

Share this post


Link to post
Share on other sites

Gran aporte a la comunidad, 

Sólo algo que noté

Haciéndolo a través de functions. 

Si se escribe el nombre de una pelicula con varias por ejemplo "Batman"
Si seleccionas "Ver todos los resultados", siempre te redirecciona al primer resultado de la opciones.  

A través de .htaccess sucede lo mismo, pero solo si pongo otra palabra que sea diferente a "search"

Saludos.

Edited by Arlin360

Share this post


Link to post
Share on other sites
On 10/13/2018 at 2:01 PM, Arlin360 said:

Great contribution to the community, 

Only something that I noticed

Doing it through functions. 

If you write the name of a movie with several for example "Batman"
If you select "See all results", it always redirects you to the first result of the options.  

Through .htaccess the same thing happens, but only if I put another word that is different from "search"

Greetings.

Hi, I didnt get your comment, anyway, i did it through functions, and the results comes out as they should with the domain/search/batman

https://picr.ws/d/ZQ0.jpg

cheers.

Share this post


Link to post
Share on other sites

×
×
  • Create New...