WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] wp_rewrite help (3 posts)

  1. camdagr8
    Member
    Posted 3 years ago #

    I'm not too familiar with wp_rewrite and need some help creating a simple rewrite. I've looked at the codex and it's very limited on rewrite help for doing something simple.

    I want to take the url:
    http://mysite.com/tutorial/chp/1

    and rewrite it to:
    http://mysite.com/tutorial/?chp=1
    The catch is that I want the url all the way up to /chp in tact so that no matter what comes before /chp/ it's used in the new url and the vars are added in.

    I have my permalinks set to the following:
    /%category%/%postname%/

    I'm trying the current rewrite:

    // URL rewrite
    
    // flush rules
    add_filter('init','wpngfb_flush');
    function wpngfb_flush() {
    	global $wp_rewrite;
    	$wp_rewrite->flush_rules();
    }
    
    // Add a new rule
    add_filter('rewrite_rules_array','wpngfb_rules');
    function wpngfb_rules($rules) {
    	$newrules = array();
    	$newrules['^(.*)/chp/([0-9]+)$'] = 'index.php?pagename=$matches[1]&chp=$matches[2]';
    	return $newrules + $rules;
    }
    
    // Add the chp var so that WP recognizes it
    add_filter('query_vars','wpngfb_var_insert');
    function wpngfb_var_insert($vars) {
    	array_push($vars, 'chp');
    	return $vars;
    }

    This works some what, but it's dropping the /chp/1 from the url completely and ignoring the var include

  2. camdagr8
    Member
    Posted 3 years ago #

    bump

  3. camdagr8
    Member
    Posted 3 years ago #

    Figured it out. There's nothing wrong with my code, you just can't apply the rewrite rules on category/posts. It must be applied to a page.

Topic Closed

This topic has been closed to new replies.

About this Topic