There’s some funny business going on when you try to add a custom rewrite rule to WordPress and it gets interpreted as an “external” rule. What’s going on?
add_rewrite_rule() is used to add rewrite rules to WordPress. It’s used to instruct WordPress to translate URLs such as
To achieve this, you use the function like this:
add_rewrite_rule( '^([^/]*)/([^/]*)/([^/]*)', 'index.php?country=$matches&city=$matches&landmark=$matches' );
There’s a nice tutorial about the function over at Pippin’s plugins.
The examples given on the Codex page are OK if you use the function to add a rewrite that direct to the
index.php file. However, if the redirect target is not the
index.php, WordPress interprets the redirect as an “external” and writes it to the
.htaccess file instead.
There are (at least) three undocumented catches here:
The redirect URL must be relative, because in the
.htaccessfile it gets preceded by a slash
/. That makes for example
plugins_url()unusable in conjunction with
$matchessyntax won’t work, because it gets written in the
.htaccessfile as such, and it is not understood by Apache.
The regex gets prepended with
.htaccess, so if it already starts with
^, you get two.