• Not sure this is where to put this, but I have a question. What I’m trying to do is the following… When someone hits https://example.com/foo/bar/ on my site, they should see the word “bar” and nothing more.. I’m simplifying all of this of course. Here’s my code. This doesn’t seem to be working.

    add_filter( 'query_vars', 'analytics_rewrite_add_var' ); 
    
    function analytics_rewrite_add_var( $vars )
    {
    	array_push( $vars, 'foo' );
    	return $vars;
    } 
    
    function add_analytic_rewrite_rule($rules){
    	add_rewrite_tag( '%foo%', '([^&]+)' );
    	add_rewrite_rule(
    		'^foo/([^/]*)/?',
    		'index.php?foo=$matches[1]',
    		'top'
    	);
    }
    add_action('rewrite_rules_array', 'add_analytic_rewrite_rule');
    add_action( 'template_redirect', 'analytics_rewrite_catch' ); 
    
    function analytics_rewrite_catch()
    {
    	global $wp_query; 
    
    	if ( array_key_exists( 'foo', $wp_query->query_vars ) ) { 
    
    	echo $_GET['foo'];
    		exit; 
    
    	}
    }
    
    function the_unique_rewrite_flush(){
    	global $wp_rewrite;
       	$wp_rewrite->flush_rules();
    }
    add_action('init','the_unique_rewrite_flush');
  • The topic ‘URL Rewrite Question’ is closed to new replies.