• Hello,

    We are trying to get some custom search links with parameters to not include the parameters and output as a ‘pretty url’.

    (the search page returns villas that are in a specific region (category)

    We would like this:
    /villa_rental_italy?region=regions-venetia

    to look like this:
    /villa_rental_italy/region/regions-venetia (or simillar)

    We have tried (and various variants) of this:

    [ Moderator note: code fixed. Please wrap code in the backtick character or use the code button. ]

    function create_new_url_querystring() {
         add_rewrite_rule(
          '^villa_rental_italy/region/([^/]*)$',
          'villa_rental_italy?region=regions-venetia&search=region',
          #'index.php?region=$1&search=region=$matches[1]',
          'top'
          );
         add_rewrite_tag('%villa_rental_italy%','([^/]*)');
         //flush_rewrite_rules();
        }
        add_action('init', 'create_new_url_querystring');

    which hasnt worked, any input would be greatly appreciated.

    many thanks Sam

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    I take it /villa_rental_italy?region=regions-venetia does what you want without modification, yes? And ‘villa_rental_italy’ is a page slug/name?

    Then the rewrite tag is incorrect. Unless you intend to use that tag in a permastruct, what I recommend is you add the ‘region’ query var to the query var whitelist by using the ‘query_vars’ filter.

    Your call to add a rewrite rule is also flawed. As long as ‘region’ is whitelisted and my initial assumptions are correct, this should do the job:

    add_rewrite_rule(
       '^villa_rental_italy/region/([^/]*)/?',
       'index.php?name=villa_rental_italy&region=$matches[1]',
       'top'
    );

Viewing 1 replies (of 1 total)
  • The topic ‘PHP rewrite url rules for custom search url to output without parameters’ is closed to new replies.