iis7_add_rewrite_rule

The timeline below displays how wordpress function iis7_add_rewrite_rule has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.1

/**
 * Adds WordPress rewrite rule to the IIS 7+ configuration file.
 *
 * @since 2.8.0
 *
 * @param string $filename     The file path to the configuration file.
 * @param string $rewrite_rule The XML fragment with URL Rewrite rule.
 * @return bool
 */
function iis7_add_rewrite_rule($filename, $rewrite_rule)
{
    if (!class_exists('DOMDocument', false)) {
        return false;
    }
    // If configuration file does not exist then we create one.
    if (!file_exists($filename)) {
        $fp = fopen($filename, 'w');
        fwrite($fp, '<configuration/>');
        fclose($fp);
    }
    $doc = new DOMDocument();
    $doc->preserveWhiteSpace = false;
    if ($doc->load($filename) === false) {
        return false;
    }
    $xpath = new DOMXPath($doc);
    // First check if the rule already exists as in that case there is no need to re-add it.
    $wordpress_rules = $xpath->query('/configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'wordpress\')] | /configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'WordPress\')]');
    if ($wordpress_rules->length > 0) {
        return true;
    }
    // Check the XPath to the rewrite rule and create XML nodes if they do not exist.
    $xml_nodes = $xpath->query('/configuration/system.webServer/rewrite/rules');
    if ($xml_nodes->length > 0) {
        $rules_node = $xml_nodes->item(0);
    } else {
        $rules_node = $doc->createElement('rules');
        $xml_nodes = $xpath->query('/configuration/system.webServer/rewrite');
        if ($xml_nodes->length > 0) {
            $rewrite_node = $xml_nodes->item(0);
            $rewrite_node->appendChild($rules_node);
        } else {
            $rewrite_node = $doc->createElement('rewrite');
            $rewrite_node->appendChild($rules_node);
            $xml_nodes = $xpath->query('/configuration/system.webServer');
            if ($xml_nodes->length > 0) {
                $system_web_server_node = $xml_nodes->item(0);
                $system_web_server_node->appendChild($rewrite_node);
            } else {
                $system_web_server_node = $doc->createElement('system.webServer');
                $system_web_server_node->appendChild($rewrite_node);
                $xml_nodes = $xpath->query('/configuration');
                if ($xml_nodes->length > 0) {
                    $config_node = $xml_nodes->item(0);
                    $config_node->appendChild($system_web_server_node);
                } else {
                    $config_node = $doc->createElement('configuration');
                    $doc->appendChild($config_node);
                    $config_node->appendChild($system_web_server_node);
                }
            }
        }
    }
    $rule_fragment = $doc->createDocumentFragment();
    $rule_fragment->appendXML($rewrite_rule);
    $rules_node->appendChild($rule_fragment);
    $doc->encoding = 'UTF-8';
    $doc->formatOutput = true;
    saveDomDocument($doc, $filename);
    return true;
}

WordPress Version: 5.4

/**
 * Add WordPress rewrite rule to the IIS 7+ configuration file.
 *
 * @since 2.8.0
 *
 * @param string $filename The file path to the configuration file
 * @param string $rewrite_rule The XML fragment with URL Rewrite rule
 * @return bool
 */
function iis7_add_rewrite_rule($filename, $rewrite_rule)
{
    if (!class_exists('DOMDocument', false)) {
        return false;
    }
    // If configuration file does not exist then we create one.
    if (!file_exists($filename)) {
        $fp = fopen($filename, 'w');
        fwrite($fp, '<configuration/>');
        fclose($fp);
    }
    $doc = new DOMDocument();
    $doc->preserveWhiteSpace = false;
    if ($doc->load($filename) === false) {
        return false;
    }
    $xpath = new DOMXPath($doc);
    // First check if the rule already exists as in that case there is no need to re-add it.
    $wordpress_rules = $xpath->query('/configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'wordpress\')] | /configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'WordPress\')]');
    if ($wordpress_rules->length > 0) {
        return true;
    }
    // Check the XPath to the rewrite rule and create XML nodes if they do not exist.
    $xmlnodes = $xpath->query('/configuration/system.webServer/rewrite/rules');
    if ($xmlnodes->length > 0) {
        $rules_node = $xmlnodes->item(0);
    } else {
        $rules_node = $doc->createElement('rules');
        $xmlnodes = $xpath->query('/configuration/system.webServer/rewrite');
        if ($xmlnodes->length > 0) {
            $rewrite_node = $xmlnodes->item(0);
            $rewrite_node->appendChild($rules_node);
        } else {
            $rewrite_node = $doc->createElement('rewrite');
            $rewrite_node->appendChild($rules_node);
            $xmlnodes = $xpath->query('/configuration/system.webServer');
            if ($xmlnodes->length > 0) {
                $system_webServer_node = $xmlnodes->item(0);
                $system_webServer_node->appendChild($rewrite_node);
            } else {
                $system_webServer_node = $doc->createElement('system.webServer');
                $system_webServer_node->appendChild($rewrite_node);
                $xmlnodes = $xpath->query('/configuration');
                if ($xmlnodes->length > 0) {
                    $config_node = $xmlnodes->item(0);
                    $config_node->appendChild($system_webServer_node);
                } else {
                    $config_node = $doc->createElement('configuration');
                    $doc->appendChild($config_node);
                    $config_node->appendChild($system_webServer_node);
                }
            }
        }
    }
    $rule_fragment = $doc->createDocumentFragment();
    $rule_fragment->appendXML($rewrite_rule);
    $rules_node->appendChild($rule_fragment);
    $doc->encoding = 'UTF-8';
    $doc->formatOutput = true;
    saveDomDocument($doc, $filename);
    return true;
}

WordPress Version: 5.1

/**
 * Add WordPress rewrite rule to the IIS 7+ configuration file.
 *
 * @since 2.8.0
 *
 * @param string $filename The file path to the configuration file
 * @param string $rewrite_rule The XML fragment with URL Rewrite rule
 * @return bool
 */
function iis7_add_rewrite_rule($filename, $rewrite_rule)
{
    if (!class_exists('DOMDocument', false)) {
        return false;
    }
    // If configuration file does not exist then we create one.
    if (!file_exists($filename)) {
        $fp = fopen($filename, 'w');
        fwrite($fp, '<configuration/>');
        fclose($fp);
    }
    $doc = new DOMDocument();
    $doc->preserveWhiteSpace = false;
    if ($doc->load($filename) === false) {
        return false;
    }
    $xpath = new DOMXPath($doc);
    // First check if the rule already exists as in that case there is no need to re-add it
    $wordpress_rules = $xpath->query('/configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'wordpress\')] | /configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'WordPress\')]');
    if ($wordpress_rules->length > 0) {
        return true;
    }
    // Check the XPath to the rewrite rule and create XML nodes if they do not exist
    $xmlnodes = $xpath->query('/configuration/system.webServer/rewrite/rules');
    if ($xmlnodes->length > 0) {
        $rules_node = $xmlnodes->item(0);
    } else {
        $rules_node = $doc->createElement('rules');
        $xmlnodes = $xpath->query('/configuration/system.webServer/rewrite');
        if ($xmlnodes->length > 0) {
            $rewrite_node = $xmlnodes->item(0);
            $rewrite_node->appendChild($rules_node);
        } else {
            $rewrite_node = $doc->createElement('rewrite');
            $rewrite_node->appendChild($rules_node);
            $xmlnodes = $xpath->query('/configuration/system.webServer');
            if ($xmlnodes->length > 0) {
                $system_webServer_node = $xmlnodes->item(0);
                $system_webServer_node->appendChild($rewrite_node);
            } else {
                $system_webServer_node = $doc->createElement('system.webServer');
                $system_webServer_node->appendChild($rewrite_node);
                $xmlnodes = $xpath->query('/configuration');
                if ($xmlnodes->length > 0) {
                    $config_node = $xmlnodes->item(0);
                    $config_node->appendChild($system_webServer_node);
                } else {
                    $config_node = $doc->createElement('configuration');
                    $doc->appendChild($config_node);
                    $config_node->appendChild($system_webServer_node);
                }
            }
        }
    }
    $rule_fragment = $doc->createDocumentFragment();
    $rule_fragment->appendXML($rewrite_rule);
    $rules_node->appendChild($rule_fragment);
    $doc->encoding = 'UTF-8';
    $doc->formatOutput = true;
    saveDomDocument($doc, $filename);
    return true;
}

WordPress Version: 5.1

/**
 * Add WordPress rewrite rule to the IIS 7+ configuration file.
 *
 * @since 2.8.0
 *
 * @param string $filename The file path to the configuration file
 * @param string $rewrite_rule The XML fragment with URL Rewrite rule
 * @return bool
 */
function iis7_add_rewrite_rule($filename, $rewrite_rule)
{
    if (!class_exists('DOMDocument', false)) {
        return false;
    }
    // If configuration file does not exist then we create one.
    if (!file_exists($filename)) {
        $fp = fopen($filename, 'w');
        fwrite($fp, '<configuration/>');
        fclose($fp);
    }
    $doc = new DOMDocument();
    $doc->preserveWhiteSpace = false;
    if ($doc->load($filename) === false) {
        return false;
    }
    $xpath = new DOMXPath($doc);
    // First check if the rule already exists as in that case there is no need to re-add it
    $wordpress_rules = $xpath->query('/configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'wordpress\')] | /configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'WordPress\')]');
    if ($wordpress_rules->length > 0) {
        return true;
    }
    // Check the XPath to the rewrite rule and create XML nodes if they do not exist
    $xmlnodes = $xpath->query('/configuration/system.webServer/rewrite/rules');
    if ($xmlnodes->length > 0) {
        $rules_node = $xmlnodes->item(0);
    } else {
        $rules_node = $doc->createElement('rules');
        $xmlnodes = $xpath->query('/configuration/system.webServer/rewrite');
        if ($xmlnodes->length > 0) {
            $rewrite_node = $xmlnodes->item(0);
            $rewrite_node->appendChild($rules_node);
        } else {
            $rewrite_node = $doc->createElement('rewrite');
            $rewrite_node->appendChild($rules_node);
            $xmlnodes = $xpath->query('/configuration/system.webServer');
            if ($xmlnodes->length > 0) {
                $system_webServer_node = $xmlnodes->item(0);
                $system_webServer_node->appendChild($rewrite_node);
            } else {
                $system_webServer_node = $doc->createElement('system.webServer');
                $system_webServer_node->appendChild($rewrite_node);
                $xmlnodes = $xpath->query('/configuration');
                if ($xmlnodes->length > 0) {
                    $config_node = $xmlnodes->item(0);
                    $config_node->appendChild($system_webServer_node);
                } else {
                    $config_node = $doc->createElement('configuration');
                    $doc->appendChild($config_node);
                    $config_node->appendChild($system_webServer_node);
                }
            }
        }
    }
    $rule_fragment = $doc->createDocumentFragment();
    $rule_fragment->appendXML($rewrite_rule);
    $rules_node->appendChild($rule_fragment);
    $doc->encoding = "UTF-8";
    $doc->formatOutput = true;
    saveDomDocument($doc, $filename);
    return true;
}

WordPress Version: 4.4

/**
 * Add WordPress rewrite rule to the IIS 7+ configuration file.
 *
 * @since 2.8.0
 *
 * @param string $filename The file path to the configuration file
 * @param string $rewrite_rule The XML fragment with URL Rewrite rule
 * @return bool
 */
function iis7_add_rewrite_rule($filename, $rewrite_rule)
{
    if (!class_exists('DOMDocument', false)) {
        return false;
    }
    // If configuration file does not exist then we create one.
    if (!file_exists($filename)) {
        $fp = fopen($filename, 'w');
        fwrite($fp, '<configuration/>');
        fclose($fp);
    }
    $doc = new DOMDocument();
    $doc->preserveWhiteSpace = false;
    if ($doc->load($filename) === false) {
        return false;
    }
    $xpath = new DOMXPath($doc);
    // First check if the rule already exists as in that case there is no need to re-add it
    $wordpress_rules = $xpath->query('/configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'wordpress\')]');
    if ($wordpress_rules->length > 0) {
        return true;
    }
    // Check the XPath to the rewrite rule and create XML nodes if they do not exist
    $xmlnodes = $xpath->query('/configuration/system.webServer/rewrite/rules');
    if ($xmlnodes->length > 0) {
        $rules_node = $xmlnodes->item(0);
    } else {
        $rules_node = $doc->createElement('rules');
        $xmlnodes = $xpath->query('/configuration/system.webServer/rewrite');
        if ($xmlnodes->length > 0) {
            $rewrite_node = $xmlnodes->item(0);
            $rewrite_node->appendChild($rules_node);
        } else {
            $rewrite_node = $doc->createElement('rewrite');
            $rewrite_node->appendChild($rules_node);
            $xmlnodes = $xpath->query('/configuration/system.webServer');
            if ($xmlnodes->length > 0) {
                $system_webServer_node = $xmlnodes->item(0);
                $system_webServer_node->appendChild($rewrite_node);
            } else {
                $system_webServer_node = $doc->createElement('system.webServer');
                $system_webServer_node->appendChild($rewrite_node);
                $xmlnodes = $xpath->query('/configuration');
                if ($xmlnodes->length > 0) {
                    $config_node = $xmlnodes->item(0);
                    $config_node->appendChild($system_webServer_node);
                } else {
                    $config_node = $doc->createElement('configuration');
                    $doc->appendChild($config_node);
                    $config_node->appendChild($system_webServer_node);
                }
            }
        }
    }
    $rule_fragment = $doc->createDocumentFragment();
    $rule_fragment->appendXML($rewrite_rule);
    $rules_node->appendChild($rule_fragment);
    $doc->encoding = "UTF-8";
    $doc->formatOutput = true;
    saveDomDocument($doc, $filename);
    return true;
}

WordPress Version: 3.7

/**
 * Add WordPress rewrite rule to the IIS 7+ configuration file.
 *
 * @since 2.8.0
 *
 * @param string $filename The file path to the configuration file
 * @param string $rewrite_rule The XML fragment with URL Rewrite rule
 * @return bool
 */
function iis7_add_rewrite_rule($filename, $rewrite_rule)
{
    if (!class_exists('DOMDocument')) {
        return false;
    }
    // If configuration file does not exist then we create one.
    if (!file_exists($filename)) {
        $fp = fopen($filename, 'w');
        fwrite($fp, '<configuration/>');
        fclose($fp);
    }
    $doc = new DOMDocument();
    $doc->preserveWhiteSpace = false;
    if ($doc->load($filename) === false) {
        return false;
    }
    $xpath = new DOMXPath($doc);
    // First check if the rule already exists as in that case there is no need to re-add it
    $wordpress_rules = $xpath->query('/configuration/system.webServer/rewrite/rules/rule[starts-with(@name,\'wordpress\')]');
    if ($wordpress_rules->length > 0) {
        return true;
    }
    // Check the XPath to the rewrite rule and create XML nodes if they do not exist
    $xmlnodes = $xpath->query('/configuration/system.webServer/rewrite/rules');
    if ($xmlnodes->length > 0) {
        $rules_node = $xmlnodes->item(0);
    } else {
        $rules_node = $doc->createElement('rules');
        $xmlnodes = $xpath->query('/configuration/system.webServer/rewrite');
        if ($xmlnodes->length > 0) {
            $rewrite_node = $xmlnodes->item(0);
            $rewrite_node->appendChild($rules_node);
        } else {
            $rewrite_node = $doc->createElement('rewrite');
            $rewrite_node->appendChild($rules_node);
            $xmlnodes = $xpath->query('/configuration/system.webServer');
            if ($xmlnodes->length > 0) {
                $system_webServer_node = $xmlnodes->item(0);
                $system_webServer_node->appendChild($rewrite_node);
            } else {
                $system_webServer_node = $doc->createElement('system.webServer');
                $system_webServer_node->appendChild($rewrite_node);
                $xmlnodes = $xpath->query('/configuration');
                if ($xmlnodes->length > 0) {
                    $config_node = $xmlnodes->item(0);
                    $config_node->appendChild($system_webServer_node);
                } else {
                    $config_node = $doc->createElement('configuration');
                    $doc->appendChild($config_node);
                    $config_node->appendChild($system_webServer_node);
                }
            }
        }
    }
    $rule_fragment = $doc->createDocumentFragment();
    $rule_fragment->appendXML($rewrite_rule);
    $rules_node->appendChild($rule_fragment);
    $doc->encoding = "UTF-8";
    $doc->formatOutput = true;
    saveDomDocument($doc, $filename);
    return true;
}