# Pretty Search URLs

> Redirects ?s=term searches to /search/term/ when pretty permalinks are enabled, which gives cleaner, shareable URLs.

- Type: PHP
- Runs: Run everywhere
- Category: [Search](https://wpide.com/library/category/search/)
- Tags: search, permalinks, urls
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/pretty-search-urls/

## Code

```php
add_action( 'template_redirect', function () {
    if ( ! is_search() || ! get_option( 'permalink_structure' ) || ! isset( $_GET['s'] ) || 1 !== count( $_GET ) ) {
        return;
    }

    $term = get_search_query( false );

    if ( '' !== $term ) {
        wp_safe_redirect( home_url( '/search/' . rawurlencode( $term ) . '/' ) );
        exit;
    }
} );
```

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Pretty Search URLs" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
