Codex tools: Log in
add_rewrite_rule() allows you to specify additional rewrite rules for WordPress. It is most commonly used in conjunction with add_rewrite_tag() (which allows WordPress to recognize custom post/get variables).
add_rewrite_rule($rule, $rewrite, $position);
Let's assume you are creating a "Nutrition" page for showing nutritional information. This page uses a custom template and takes two variables, food and variety. Instead of passing ugly querystring variables to the page, you can set up a rewrite rule to create some custom pretty URLs. See below...
This example would match a requested URL like this:
...and interpret it to actually mean...
NOTE: When using $matches to retrieve the values of a matched URL, capture group data starts at 1, not 0.
IMPORTANT: By default, WordPress will not recognize custom querystring variables used for rewrites. You must register your querystring variables with WordPress. Simply use add_rewrite_tag() to do this, or the above rewrite will not work! More information about capturing querystring variable values after a rewrite can be found here.
add_rewrite_rule() is located in