• I have redirected search page to new pretty URL, but that page now shows error 404. What extra code do I have to add to deal with URL on that error page.

    Another question is, how to check if re-writing is done and custom_rewrite_rule() (see below) is functioning properly.

    Here is my code so far:

    add_action( 'template_redirect', 'change_search_url_rewrite' );
    
    function change_search_url_rewrite() {
        if( isset( $_GET['search_lat'] ) || isset( $_GET['search_lng'] ) || isset( $_GET['search_city'] ) )
        {
            $new_uri    = array( );
    
            if( isset( $_GET['search_lat'] ) && strlen( $_GET['search_lat'] ) )
            {
                $new_uri[]  = urlencode( 'lat-'.$_GET['search_lat'] );
            }
    
            if( isset( $_GET['search_lng'] ) && strlen( $_GET['search_lng'] ) )
            {
                $new_uri[]  = urlencode( 'lng-'.$_GET['search_lng'] );
            }
    
            if( isset( $_GET['sort'] ) && strlen( $_GET['sort'] ) )
            {
                $new_uri[]  = urlencode( 'sort-'.$_GET['sort'] );
            }
    
            if( isset( $_GET['search_city'] ) && strlen( $_GET['search_city'] ) )
            {
                $new_uri[]  = urlencode( 'city-'.$_GET['search_city'] );
            }
    
            if( isset( $_GET['search_category'] ) && strlen( $_GET['search_category'] ) )
            {
                $new_uri[]  = urlencode( 'category-'.$_GET['search_category'] );
            }
    
            if( isset( $_GET['search_type'] ) && strlen( $_GET['search_type'] ) )
            {
                $new_uri[]  = urlencode( 'type-'.$_GET['search_type'] );
            }
    
            if( isset( $_GET['search_neighborhood'] ) && strlen( $_GET['search_neighborhood'] ) )
            {
                $new_uri[]  = urlencode( 'neighborhood-'.$_GET['search_neighborhood'] );
            }
    
            wp_redirect( home_url( "/properties-search-results-2/" ) . implode( '/' , $new_uri ) );
    
            exit();
        }
    }
    
    add_action('init', 'custom_rewrite_rule', 10, 0);
    
    function custom_rewrite_rule()
    {
        add_rewrite_rule(   "properties-search-results-2/category-([^/]*)/city-([^/]*)/lat-([^/]*)/lng-([^/]*)/neighborhood-([^/]*)/sort-([^/]*)/type-([^/]*)/?" ,
                            '?page_id=16&search_lat=$matches[3]&search_lng=$matches[4]&sort=$matches[6]&search_city=$matches[2]&search_category=$matches[1]&search_type=$matches[7]&search_neighborhood=$matches[5]' ,
                            'top' );
    }

    The page at /properties-search-results-2/ has page id of 16, so I hard coded it to rewrite function. The search was working on this page before rewriting.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter hamdusa

    (@hamdusa)

    Any help?

    Moderator bcworkz

    (@bcworkz)

    If the rewrite rule were working correctly you would not be getting the 404, so let’s just focus on that. The $redirect parameter needs to have a destination file, usually index.php, so index.php?page_id=16&search_lat=$matches[3]// etc...

    The only way I know to test it’s working is to type the permalink into your browser and see if the result is correct. Note that in order for rewrite rules to be applied you need to toggle your permalink settings off then back on again.

    FYI, it’s actually counter productive to bump in these forums. We’re all volunteers here with limited time, especially on weekends when we’re not “working ;)”, so response time can be slow. All the regulars here work off the “no replies” filter to see who needs help. By bumping you’ve essentially replied to yourself, giving the appearance you’ve been helped. I only found your topic because I am a curious sort who often reads other’s replies.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘How to change search query string to permalink?’ is closed to new replies.