Problem with Custom rewrite rule in WordPress

Posted: October 30, 2010 in Wordpress

I was working with rewriting a custom rule for my wordpress plugin and i had a strange problem.

function aff_add_rewrite_rules( $wp_rewrite )
  $new_rules = array(
     '^redirect/([0-9]+)/$' => 'index.php?go=$matches[1]'); //question mark ? resolves the problem

  $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;

The above rewrite rule

^redirect/([0-9]+)/$’ => ‘index.php?go=$matches[1]

simply refused to work, but it works for

^redirect/(.+)$’ => ‘index.php?go=$matches[1]

(.+) means anything including alphabets and numbers.

I only wanted the rewrite rule to accept numbers, not just anything and after researching a log, i found that the culprit was ? (question mark).

^redirect/?([0-9]+)/?$’ => ‘index.php?go=$matches[1]

and bingo! it worked! I never used the ? sign in htaccess rules and it appears to be wordpress needs it to parse the rewrite rules properly.

You can always refer to the list of wordpress 2.x rewrite rules while writing your custom rule.

  1. As already mentioned above, all the rewriting rules are applied to the Substitution (in the order of definition in the config file). Url Shortening Service

  2. hey, nice weblog and good publish

  3. narnia says:

    great post, just the kind of information I was looking for

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s