Using WordPress ‘iis7_url_rewrite_rules’ PHP filter

The iis7_url_rewrite_rules WordPress PHP Filter allows you to modify the list of rewrite rules formatted for output to a web.config file in an IIS environment.

Usage

add_filter('iis7_url_rewrite_rules', 'my_custom_rewrite_rules');
function my_custom_rewrite_rules($rules) {
    // your custom code here
    return $rules;
}

Parameters

  • $rules (string) – The rewrite rules formatted for IIS web.config.

More information

See WordPress Developer Resources: iis7_url_rewrite_rules

Examples

Add a custom rewrite rule

Add a custom rewrite rule to the IIS web.config.

add_filter('iis7_url_rewrite_rules', 'add_my_custom_rewrite_rule');
function add_my_custom_rewrite_rule($rules) {
    $custom_rule = "\n<rule name=\"CustomRule\" stopProcessing=\"true\">\n";
    $custom_rule .= "  <match url=\"^custom-path/([0-9]+)/?$\" ignoreCase=\"false\" />\n";
    $custom_rule .= "  <action type=\"Rewrite\" url=\"index.php?pagename=custom-page&custom_id={R:1}\" />\n";
    $custom_rule .= "</rule>";

    $rules .= $custom_rule;

    return $rules;
}

Remove a specific rule

Remove a specific rule from the IIS web.config by searching for the rule name.

add_filter('iis7_url_rewrite_rules', 'remove_specific_rule');
function remove_specific_rule($rules) {
    $rule_name = "RuleToRemove";
    $rule_start = strpos($rules, "<rule name=\"$rule_name\"");
    $rule_end = strpos($rules, "</rule>", $rule_start) + 7;

    if ($rule_start !== false && $rule_end !== false) {
        $rules = substr_replace($rules, '', $rule_start, $rule_end - $rule_start);
    }

    return $rules;
}

Replace a rule with a custom rule

Replace an existing rule in the IIS web.config with a custom rule.

add_filter('iis7_url_rewrite_rules', 'replace_existing_rule');
function replace_existing_rule($rules) {
    $old_rule_name = "OldRule";
    $new_rule = "<rule name=\"NewRule\" stopProcessing=\"true\">\n";
    $new_rule .= "  <match url=\"^new-path/([0-9]+)/?$\" ignoreCase=\"false\" />\n";
    $new_rule .= "  <action type=\"Rewrite\" url=\"index.php?pagename=new-page&new_id={R:1}\" />\n";
    $new_rule .= "</rule>";

    $rule_start = strpos($rules, "<rule name=\"$old_rule_name\"");
    $rule_end = strpos($rules, "</rule>", $rule_start) + 7;

    if ($rule_start !== false && $rule_end !== false) {
        $rules = substr_replace($rules, $new_rule, $rule_start, $rule_end - $rule_start);
    }

    return $rules;
}

Disable all rewrite rules

Disable all rewrite rules in the IIS web.config.

add_filter('iis7_url_rewrite_rules', 'disable_all_rewrite_rules');
function disable_all_rewrite_rules($rules) {
    return '';
}

Add a custom condition to an existing rule

Add a custom condition to an existing rule in the IIS web.config.

add_filter('iis7_url_rewrite_rules', 'add_custom_condition_to_existing_rule');
function add_custom_condition_to_existing_rule($rules) {
    $rule_name = "ExistingRule";
    $custom_condition = "  <conditions>\n    <add input=\"{REQUEST_URI}\" pattern=\"^/exclude-path/\" negate=\"true\" />\n  </conditions>\n";

    $rule_start = strpos($rules, "<rule name=\"$rule_name\"");
    $action_start = strpos($rules, "<action", $rule_start);

    if ($rule_start !== false && $action_start !== false) {
        $rules = substr_replace($rules, $custom_condition, $action_start, 0);
    }

    return $rules;
}

This example adds a custom condition to the rule with the name “ExistingRule” that will exclude the rewrite rule from being applied if the request URI starts with /exclude-path/.