I know that I can edit .htaccess manually and add a RewriteCond statement inside, but I need to build a plugin that does this the proper way by using WP rewrites which then get pushed into .htaccess with flush_rewrite_rules(). I need RewriteCond in this case.
Does WP have some obscure function that lets me push a RewriteCond statement into the .htaccess?
In short yes…
‘External’ Rewrite Rules
If in
add_rewrite_rule
the rule is isn’t directed toindex.php
then the rule is treated as an ‘external’ rule (i.e. not to be processed by WordPress’ internal handing of rewrites) and instead written to the.htaccess
file. I.e. if:is used instead of
Then the rule is recognised as a non (internal) WordPress rule and is added the .htaccess file rather than dealt with internally. Note for ‘external’ rules – you use
$1
instead of$matches[1]
;Adding custom rules to .htaccess
When rewrite rules are flushed, the
.htacess
file is written to. There is a hook which filters what is actually written.mod_rewrite_rules
The codex has this to say: