Detecting mod_rewrite Module Status in Apache and IIS Using PHP

Nov 23, 2025 · Programming · 11 views · 7.8

Keywords: PHP | Apache | mod_rewrite | IIS | URL_rewrite

Abstract: This article comprehensively examines various methods for detecting the enabled status of the mod_rewrite module in Apache and IIS server environments using PHP. It focuses on the direct detection approach using the apache_get_modules() function and the indirect detection technique via shell_exec() system commands. The paper also introduces auxiliary detection methods through environment variable settings and phpinfo() pages, providing complete code examples and applicability analysis under different server configurations to help developers choose the most suitable detection solution based on their specific environment.

Detecting mod_rewrite Module in Apache Server

In Apache server environments, when using mod_php as the PHP processor, you can directly detect enabled Apache modules through PHP built-in functions. The specific implementation code is as follows:

<?php
if (in_array('mod_rewrite', apache_get_modules())) {
    echo "mod_rewrite module is enabled";
} else {
    echo "mod_rewrite module is not enabled";
}
?>

This method is straightforward but limited to mod_php environments. When PHP runs in CGI mode, the apache_get_modules() function is unavailable, requiring alternative detection approaches.

Detecting mod_rewrite via System Commands

For CGI mode or situations requiring more universal detection methods, you can check Apache compiled modules by executing system commands. Here is the specific implementation code:

<?php
$output = shell_exec('/usr/local/apache/bin/apachectl -l');
if (strpos($output, 'mod_rewrite') !== false) {
    echo "mod_rewrite module is enabled";
} else {
    echo "mod_rewrite module is not enabled";
}
?>

It's important to note that the apachectl command path may vary depending on server configuration, and the path parameter should be adjusted according to the specific environment in practical applications.

Environment Variable Detection Method

Another detection approach involves setting environment variables in the .htaccess file and then checking for the existence of these variables in PHP code. First, add the following configuration to the .htaccess file:

<IfModule mod_rewrite.c>
    SetEnv HTTP_MOD_REWRITE On
    RewriteEngine on
    # Other rewrite rules...
</IfModule>

Then perform the detection in PHP code:

<?php
if (array_key_exists('HTTP_MOD_REWRITE', $_SERVER)) {
    echo "mod_rewrite module is enabled";
} else {
    echo "mod_rewrite module is not enabled";
}
?>

Detection Using phpinfo() Page

The most intuitive detection method involves creating a phpinfo page and confirming mod_rewrite status by checking the Loaded Modules section:

<?php
phpinfo();
?>

Search for "mod_rewrite" in the generated page; if found, it indicates the module is enabled. While this method is simple, it requires manual inspection and is unsuitable for automated detection scenarios.

URL Rewrite Detection in IIS Server

For IIS servers, although URL rewrite modules similar to mod_rewrite exist, detection methods differ. Detection can be implemented by checking specific extensions or environment variables:

<?php
// Check if IIS URL Rewrite Module is available
if (isset($_SERVER['IIS_UrlRewriteModule'])) {
    echo "URL rewrite module is enabled";
} else {
    // Additional detection logic
    echo "Further detection required";
}
?>

In practical applications, it's recommended to combine multiple detection methods to ensure accurate determination of rewrite module status across different server environments.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.