How to turn this .htaccess rule into a dynamic rule with add_rewrite_rule, et al?

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

Read More

$_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.

Related posts

1 comment

  1. 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.

Comments are closed.