Using WordPress ‘iis7_save_url_rewrite_rules()’ PHP function

The iis7_save_url_rewrite_rules() WordPress PHP function updates the IIS web.config file with the current rules if it is writable. If the permalinks do not require rewrite rules, then the rules are deleted from the web.config file.

Usage

iis7_save_url_rewrite_rules();

Parameters

  • None

More information

See WordPress Developer Resources: iis7_save_url_rewrite_rules()

Examples

Update IIS Rewrite Rules

In this example, we’ll call the iis7_save_url_rewrite_rules() function after updating the permalink structure to make sure the IIS web.config file is updated accordingly.

// Update permalink structure
update_option('permalink_structure', '/%postname%/');

// Save IIS rewrite rules
iis7_save_url_rewrite_rules();

Delete IIS Rewrite Rules

In this example, we’ll delete the IIS rewrite rules by calling the iis7_save_url_rewrite_rules() function after setting the permalink structure to the default.

// Set permalink structure to default
update_option('permalink_structure', '');

// Delete IIS rewrite rules
iis7_save_url_rewrite_rules();

Add Custom Rewrite Rule and Update IIS Rewrite Rules

In this example, we’ll add a custom rewrite rule and call the iis7_save_url_rewrite_rules() function to update the IIS web.config file.

// Add custom rewrite rule
add_rewrite_rule('^product/([0-9]+)/?', 'index.php?product_id=$matches[1]', 'top');

// Update IIS rewrite rules
iis7_save_url_rewrite_rules();

Flush Rewrite Rules and Update IIS Rewrite Rules

In this example, we’ll flush the rewrite rules and call the iis7_save_url_rewrite_rules() function to update the IIS web.config file.

// Flush rewrite rules
flush_rewrite_rules();

// Update IIS rewrite rules
iis7_save_url_rewrite_rules();

Update IIS Rewrite Rules on Theme Activation

In this example, we’ll update the IIS rewrite rules when a new theme is activated.

function my_theme_rewrite_rules() {
    // Update IIS rewrite rules
    iis7_save_url_rewrite_rules();
}
add_action('after_switch_theme', 'my_theme_rewrite_rules');