The got_mod_rewrite() WordPress PHP function checks if the server is running Apache with the mod_rewrite module loaded.
Usage
To use the got_mod_rewrite() function, simply call it and store the result in a variable:
$has_mod_rewrite = got_mod_rewrite();
Parameters
- None
More information
See WordPress Developer Resources: got_mod_rewrite
Examples
Check if mod_rewrite is enabled
Check if the mod_rewrite module is enabled and display a message accordingly.
$has_mod_rewrite = got_mod_rewrite(); if ($has_mod_rewrite) { echo "The mod_rewrite module is enabled."; } else { echo "The mod_rewrite module is not enabled."; }
Enable a feature only if mod_rewrite is available
Conditionally enable a feature in your WordPress plugin or theme only if mod_rewrite is available.
function my_custom_feature() { if (got_mod_rewrite()) { // Your feature code here } }
Add a rewrite rule if mod_rewrite is enabled
Add a custom rewrite rule to WordPress only if the mod_rewrite module is available.
function my_custom_rewrite_rule() { if (got_mod_rewrite()) { add_rewrite_rule('my-custom-rule/?$', 'index.php?custom_variable=value', 'top'); } } add_action('init', 'my_custom_rewrite_rule');
Display mod_rewrite status in the admin dashboard
Create a simple WordPress dashboard widget that displays the mod_rewrite status.
function mod_rewrite_dashboard_widget() { $status = got_mod_rewrite() ? 'Enabled' : 'Not Enabled'; echo "mod_rewrite status: $status"; } function register_mod_rewrite_dashboard_widget() { wp_add_dashboard_widget('mod_rewrite_status', 'mod_rewrite Status', 'mod_rewrite_dashboard_widget'); } add_action('wp_dashboard_setup', 'register_mod_rewrite_dashboard_widget');
Require mod_rewrite for a plugin
Prevent a WordPress plugin from being activated if mod_rewrite is not enabled.
function my_plugin_activation_check() { if (!got_mod_rewrite()) { deactivate_plugins(plugin_basename(__FILE__)); wp_die('This plugin requires the mod_rewrite module to be enabled.'); } } register_activation_hook(__FILE__, 'my_plugin_activation_check');