Does add_rewrite_rule(..) only accept “index.php” as redirect target?

There are two different rewrite systems at play when you have pretty permalinks enabled.

The first is Apache’s mod_rewrite, which basically sends any request that isn’t for a physical directory or file to the main WordPress index.php file.

The second system is internal to WordPress, and handles conversion of pretty permalinks into their component query vars, so the database can be queried for the requested content.

Now, your question: Does add_rewrite_rule(..) only accept “index.php” as redirect target?

No, you can add both kinds of rules via add_rewrite_rule. WordPress will look for index.php and add that rule to the internal rules, or will otherwise add it to the array of external rules.

So, if that’s the case, why isn’t your rule working? First, verify that the rule hasn’t made it into the .htaccess file. For new rules to be written to .htaccess, the rules need to be flushed, but not an ordinary flush. flush_rewrite_rules accepts a single boolean argument, which is false by default. Setting that to true will cause WordPress to regenerate the .htaccess file, and your rule should appear.

Leave a Comment