1

In .htaccess, I have this:

RewriteRule ^land/(.*) /?land=1&exper=$1 [QSA,L]

which makes sth like this: domain.com/land/exper1 fill in the $_GET with

$_GET['land'] = 1 and $_GET['exper'] = exper1

Great.

I want to take this out of .htaccess and add the rule dynamically, that is, look for other querystring values like 'ocean' instead of 'land'. I want the values to be filled into $_GET or some other var where I can evaluate them. I cant figure out if add_rewrite_rule is sposed to actually overwrite my .htaccess, but all the things i've tried, flush_rewrite_rules(true), etc arent doing it.

0

Try this and follow the same structure to add more rewrite rules or query vars:

add_filter('query_vars', 'my_query_vars');
function my_query_vars( $vars) {
    $vars[] = "land";
    $vars[] = "expert";
   return $vars;
}

// Add the new rewrite rule to existings ones
add_action('init','my_rewrite_rules');
function my_rewrite_rules() {
    add_rewrite_rule( 'land/(.+)/?$' , 'index.php?land=1&expert=$matches[1]' , 'top' );
}

Don't forget to flush the rewrite rules by going to the admin area->Settings->permalinks->click save.

  • And then I can access those vars with $wp_query->query_vars['land'] Thanks! – Doug Cassidy Sep 5 '13 at 19:08
  • @DougCassidy $wp_query->get('land') do the same and you write less ;) – gmazzap Sep 5 '13 at 19:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.