WordPress.org

Forums

troubles using add_rewrite_rule to change url (1 post)

  1. marcelgd
    Member
    Posted 10 months ago #

    Hi,
    I'm struggling with the add_rewrite_rule function but I can't make it work.
    Details:
    I'm working on a plugin related to real estate, this plugin adds a page to wordpress where users can perform a search, the result set is shown in that same page. The plugin is currently working fine using the regular querystring (?param1=value1&... etc), but I need to make the search url more SEO friendly. At this point I'm trying to rewrite the url into a pattern I choosed, but there's my problem, I can't make it happen.

    Current url:
    my-page?query=miami&bedrooms=2

    Desired url:
    my-page/2-bedrooms/miami

    where "miami" is the searched term, and "2" is the number of bathrooms the user entered in the corresponding box. I have more parameters, but this is just an example.

    Now, I'm using add_rewrite_rule like this:

    function my_url_rewrite(){<br />
    	add_rewrite_rule('^my-page/([0-9])+-bedrooms', 'my-page?bedrooms=$matches[1]', 'top');<br />
    }
    add_action('init', 'my_url_rewrite');<br />

    but I get a Error 404 - page not found

    What am I doing wrong?
    Thanks in advance

Reply

You must log in to post.

About this Topic