WordPress rewrite rules


Well, few weeks back, I was working on this particular WordPress project. It was previously developed by third party vendor and later it came to us for further maintenance and enhancements.

I got to say, I didn’t like the way project was developed by those people. We got no requirement specifications and zero knowledge transfers. I would say, it was best example of a totally messed up code and one heck of the system. Just to give you an idea, I would like to give you an example of problem. Whenever, I went into the wp-admin and hit the save permalink button, the site used to go down and system used to show 404 for every page. This only one example of the many such totally weird and mind twisting things.

So, that site was having some serious permalink issues on which we were trying to find out a ‘permanent’ solution. It was becoming headache to manage the permalinks manually. Every time I used to add new taxonomy or custom post type, I have had to add around 16 permalink rules manually, not all were required but I didn’t want to take any chances of getting it broken. Not only for taxonomies and CPTs but new pages also needed manual permalink settings.

By using WordPress function add_rewrite_rule you can write the rewrite rule like this:

<?php

/* add_rewrite_rule($regex, $redirect, $after); */

add_rewrite_rule('^nutrition/([^/]*)/([^/]*)/?','index.php?page_id=12&food=$matches[1]&variety=$matches[2]','top');

?>

But, in this case this didn’t help us. So, we have had to put the rewrite rules in wp_rewrite_rules() which is located in /wp-includes/rewrite.php. I know, we never have to touch the core files and that’s not right but it was a quick fix which did work and made us through.

Example of custom rules is shown below. I hope you never have to go through such odd things.

<?php

function wp_rewrite_rules() {
	$this->rules = get_option('rewrite_rules');

	if ( empty($this->rules) ) {
		$this->matches = 'matches';
		$this->rewrite_rules();
		update_option('rewrite_rules', $this->rules);
	}

	$my_new_rule = array( '^mypage/?$' => 'index.php?page_id=52989' );
	$this->rules = $my_new_rule + $this->rules;

	$my_new_rule = array( '^path/to/my/new/page/([^/]*)/?/page/?([0-9]{1,})/?$' => 'index.php?page_id=29020&categoryID=$matches[1]&paged=$matches[2]' );
	$this->rules = $my_new_rule + $this->rules;

	// Rules for custom post type
	$my_new_rule = array( '^mynewcpt/[^/]+/attachment/([^/]+)/?$' => 'index.php?attachment=$matches[1]' );
	$this->rules = $my_new_rule + $this->rules;

	$my_new_rule = array( '^mynewcpt/[^/]+/attachment/([^/]+)/trackback/?$' => 'index.php?attachment=$matches[1]&tb=1' );
	$this->rules = $my_new_rule + $this->rules;

	$my_new_rule = array( '^mynewcpt/[^/]+/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?attachment=$matches[1]&feed=$matches[2]' );
	$this->rules = $my_new_rule + $this->rules;
	
	$my_new_rule = array( '^mynewcpt/[^/]+/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?attachment=$matches[1]&feed=$matches[2]' );
	$this->rules = $my_new_rule + $this->rules;

	$my_new_rule = array( '^mynewcpt/[^/]+/attachment/([^/]+)/comment-page-([0-9]{1,})/?$' => 'index.php?attachment=$matches[1]&cpage=$matches[2]' );
	$this->rules = $my_new_rule + $this->rules;

	$my_new_rule = array( '^mynewcpt/([^/]+)/trackback/?$' => 'index.php?mynewcpt=$matches[1]&tb=1' );
	$this->rules = $my_new_rule + $this->rules;

	$my_new_rule = array( '^mynewcpt/([^/]+)/page/?([0-9]{1,})/?$' => 'index.php?mynewcpt=$matches[1]&paged=$matches[2]' );
	$this->rules = $my_new_rule + $this->rules;

	$my_new_rule = array( '^mynewcpt/([^/]+)/comment-page-([0-9]{1,})/?$' => 'index.php?mynewcpt=$matches[1]&cpage=$matches[2]' );
	$this->rules = $my_new_rule + $this->rules;

	$my_new_rule = array( '^mynewcpt/([^/]+)(/[0-9]+)?/?$' => 'index.php?mynewcpt=$matches[1]&page=$matches[2]' );
	$this->rules = $my_new_rule + $this->rules;

	$my_new_rule = array( '^mynewcpt/[^/]+/([^/]+)/?$' => 'index.php?attachment=$matches[1]' );
	$this->rules = $my_new_rule + $this->rules;

	$my_new_rule = array( '^mynewcpt/[^/]+/([^/]+)/trackback/?$' => 'index.php?attachment=$matches[1]&tb=1' );
	$this->rules = $my_new_rule + $this->rules;

	$my_new_rule = array( '^mynewcpt/category/([^/]*)/?$' => 'index.php?mynewcpt=$matches[1]&categoryID=$matches[1]' );
	$this->rules = $my_new_rule + $this->rules;

	$my_new_rule = array( '^mynewcpt/category/([^/]*)/?/page/?([0-9]{1,})/?$' => 'index.php?mynewcpt=$matches[1]&categoryID=$matches[1]&paged=$matches[2]' );
	$this->rules = $my_new_rule + $this->rules;

	return $this->rules;
}

?>

Leave a Reply

Your email address will not be published.