iis7_add_rewrite_rule

Add WordPress rewrite rule to the IIS 7+ configuration file.

Description

iis7_add_rewrite_rule( (string) $filename, (string) $rewrite_rule ); 

Parameters (2)

0. $filename (string)
The file path to the configuration file
1. $rewrite_rule (string)
The XML fragment with URL Rewrite rule

Usage

  1. if ( !function_exists( 'iis7_add_rewrite_rule' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/misc.php'; 
  3.  
  4. // The file path to the configuration file 
  5. $filename = ''; 
  6.  
  7. // The XML fragment with URL Rewrite rule 
  8. $rewrite_rule = ''; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = iis7_add_rewrite_rule($filename, $rewrite_rule); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-admin/includes/misc.php  
  1. function iis7_add_rewrite_rule($filename, $rewrite_rule) { 
  2. if ( ! class_exists( 'DOMDocument', false ) ) { 
  3. return false; 
  4.  
  5. // If configuration file does not exist then we create one. 
  6. if ( ! file_exists($filename) ) { 
  7. $fp = fopen( $filename, 'w'); 
  8. fwrite($fp, '<configuration/>'); 
  9. fclose($fp); 
  10.  
  11. $doc = new DOMDocument(); 
  12. $doc->preserveWhiteSpace = false; 
  13.  
  14. if ( $doc->load($filename) === false ) 
  15. return false; 
  16.  
  17. $xpath = new DOMXPath($doc); 
  18.  
  19. // First check if the rule already exists as in that case there is no need to re-add it 
  20. $wordpress_rules = $xpath->query('/configuration/system.webServer/rewrite/rules/rule[starts-with(@name, \'wordpress\')] | /configuration/system.webServer/rewrite/rules/rule[starts-with(@name, \'WordPress\')]'); 
  21. if ( $wordpress_rules->length > 0 ) 
  22. return true; 
  23.  
  24. // Check the XPath to the rewrite rule and create XML nodes if they do not exist 
  25. $xmlnodes = $xpath->query('/configuration/system.webServer/rewrite/rules'); 
  26. if ( $xmlnodes->length > 0 ) { 
  27. $rules_node = $xmlnodes->item(0); 
  28. } else { 
  29. $rules_node = $doc->createElement('rules'); 
  30.  
  31. $xmlnodes = $xpath->query('/configuration/system.webServer/rewrite'); 
  32. if ( $xmlnodes->length > 0 ) { 
  33. $rewrite_node = $xmlnodes->item(0); 
  34. $rewrite_node->appendChild($rules_node); 
  35. } else { 
  36. $rewrite_node = $doc->createElement('rewrite'); 
  37. $rewrite_node->appendChild($rules_node); 
  38.  
  39. $xmlnodes = $xpath->query('/configuration/system.webServer'); 
  40. if ( $xmlnodes->length > 0 ) { 
  41. $system_webServer_node = $xmlnodes->item(0); 
  42. $system_webServer_node->appendChild($rewrite_node); 
  43. } else { 
  44. $system_webServer_node = $doc->createElement('system.webServer'); 
  45. $system_webServer_node->appendChild($rewrite_node); 
  46.  
  47. $xmlnodes = $xpath->query('/configuration'); 
  48. if ( $xmlnodes->length > 0 ) { 
  49. $config_node = $xmlnodes->item(0); 
  50. $config_node->appendChild($system_webServer_node); 
  51. } else { 
  52. $config_node = $doc->createElement('configuration'); 
  53. $doc->appendChild($config_node); 
  54. $config_node->appendChild($system_webServer_node); 
  55.  
  56. $rule_fragment = $doc->createDocumentFragment(); 
  57. $rule_fragment->appendXML($rewrite_rule); 
  58. $rules_node->appendChild($rule_fragment); 
  59.  
  60. $doc->encoding = "UTF-8"; 
  61. $doc->formatOutput = true; 
  62. saveDomDocument($doc, $filename); 
  63.  
  64. return true;