152

I was wondering if it is possible to check if mod_rewrite is enabled on Apache AND IIS in PHP.

ModRewrite for IIS exists. Check it here.

So, I'm looking for a PHP script that checks for mod_rewrite on Apache and IIS.

Does anyone know such script or can write one?

Especially for Microsoft IIS.

Thanks!

Community
  • 1
  • 1
Ties
  • 1,776
  • 2
  • 12
  • 17

15 Answers15

127

If you're using mod_php, you can use apache_get_modules(). This will return an array of all enabled modules, so to check if mod_rewrite is enabled, you could simply do

in_array('mod_rewrite', apache_get_modules());

Unfortunately, you're most likely trying to do this with CGI, which makes it a little bit more difficult.

You can test it using the following, though

strpos(shell_exec('/usr/local/apache/bin/apachectl -l'), 'mod_rewrite') !== false

If the above condition evaluates to true, then mod_write is enabled.

Deep Kakkar
  • 5,831
  • 4
  • 39
  • 75
kba
  • 19,333
  • 5
  • 62
  • 89
  • 1
    I don't know. This tests for if the module `mod_rewrite` is installed. The _IIS Mod-Rewrite_ module you're probably referring to is an entirely different and commercial product - it has no association with the Apache module, it's an entirely different question and I have no experience using it. – kba Jan 26 '12 at 16:36
  • That's true, so there is properly no general script? – Ties Jan 26 '12 at 16:38
  • @DrCord Then I'm guessing you're using it in CGI. – kba Aug 07 '13 at 21:16
  • nope, was using it in a php script just like the answer and the answer below. if( ! function_exists('apache_get_modules') ){ phpinfo(); die; } always is true on my server... – DrCord Aug 07 '13 at 23:35
  • Your PHP script could very easily be CGI. – kba Aug 07 '13 at 23:45
  • @kba I dont know what path to pass in my shared server to shell_exec, I tried getting it through SHELL command but i dont have permissions for this and $_SERVER['path'] is not working at all. – Ravi Soni Aug 20 '13 at 05:05
  • Does this return true if the module is installed, or enabled? For me it's returning true as the module is installed, but disabled. – S.. Jun 30 '14 at 17:57
  • thanks, here is the full code: – Thorsten Staerk Nov 08 '14 at 16:27
  • Why not go with ``? It shows really important data, including Apache modules loaded –  Jun 02 '15 at 02:30
  • 2
    @Gerep `phpinfo()` can be useful for many things, but if you want to write a system that uses `mod_rewrite` if it's enabled or otherwise fallbacks to some other behaviour, it be useful to detect it programmatically. – kba Jun 02 '15 at 02:41
  • tried `strpos(shell_exec('/usr/local/apache/bin/apachectl -l'), 'mod_rewrite') !== false` it give `-bash: syntax error near unexpected token `shell_exec'` – Kangarooo Oct 04 '15 at 21:45
96

Copy this piece of code and run it to find out.


<?php
 if(!function_exists('apache_get_modules') ){ phpinfo(); exit; }
 $res = 'Module Unavailable';
 if(in_array('mod_rewrite',apache_get_modules())) 
 $res = 'Module Available';
?>
<html>
<head>
<title>A mod_rewrite availability check !</title></head>
<body>
<p><?php echo apache_get_version(),"</p><p>mod_rewrite $res"; ?></p>
</body>
</html>
Community
  • 1
  • 1
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
54

I like Christian Roy's solution:

###  .htaccess

<IfModule mod_rewrite.c>

    # Tell PHP that the mod_rewrite module is ENABLED.
    SetEnv HTTP_MOD_REWRITE On

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    # The rest of your rewrite rules here

</IfModule>

Then, you can check in your PHP code for

    array_key_exists('HTTP_MOD_REWRITE', $_SERVER);

No idea if this works also with IIS (I have no way to check) but the odds are good.

William Entriken
  • 37,208
  • 23
  • 149
  • 195
a.l.e
  • 699
  • 6
  • 4
35

Upload a file called info.php with this code and run it:

<?php 
phpinfo();

Search for mod_rewrite on the page, and see if you can find it under Loaded Modules.

Johnny Jensen
  • 353
  • 3
  • 4
28

don't make it so difficult you can simply find in phpinfo();

enter image description here

Hope helpful!

Thanks

Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50
loyola
  • 3,905
  • 2
  • 24
  • 18
11

via command line we in centOs we can do this

httpd -l
h0mayun
  • 3,466
  • 31
  • 40
7
<?php
phpinfo();
?>

Look under Configuration in the apache2handler in the Loaded Modules row.

This is simple and works.

<?php foreach( apache_get_modules() as $module ) echo "$module<br />";  ?>
user1649798
  • 873
  • 7
  • 8
7

This is my current method of checking if Mod_rewrite enabled for both Apache and IIS

/**
 * --------------------------------------------------------------
 *  MOD REWRITE CHECK
 * --------------------------------------------------------------
 *                                        - By A H Abid
 * Define Constant for MOD REWRITE
 * 
 * Check if server allows MOD REWRITE. Checks for both 
 * Apache and IIS.
 * 
 */
if( function_exists('apache_get_modules') && in_array('mod_rewrite',apache_get_modules()) )
    $mod_rewrite = TRUE;
elseif( isset($_SERVER['IIS_UrlRewriteModule']) )
    $mod_rewrite = TRUE;
else
    $mod_rewrite = FALSE;
define('MOD_REWRITE', $mod_rewrite);

It works in my local machine and also worked in my IIS based webhost. However, on a particular apache server, it didn't worked for Apache as the apache_get_modules() was disabled but the mod_rewrite was enable in that server.

Ahmedul Haque Abid
  • 550
  • 1
  • 5
  • 13
3

Two lines of code:

$isEnabled = in_array('mod_rewrite', apache_get_modules());
echo ($isEnabled) ? 'Enabled' : 'Not enabled';
Dan Rosenstark
  • 68,471
  • 58
  • 283
  • 421
Amal Murali
  • 75,622
  • 18
  • 128
  • 150
3

You can get a list of installed apache modules, and check against that. Perhaps you can check if its installed by searching for its .dll (or linux equivalent) file.

tolgamorf
  • 809
  • 6
  • 23
TJHeuvel
  • 12,403
  • 4
  • 37
  • 46
1

Another idea, indeed more a dirty hack, regarding mod rewrite is server dependend an not necessary a php issue: Why not, if you have the possibillity, create a test directory put a .htaccess in it rewriting to test.php, call the directory via http and check if you get the expected result you put in test.php.

Indeed, dirty.

webfan
  • 109
  • 1
  • 2
  • I created a library that builds on this exact idea: https://github.com/rosell-dk/htaccess-capability-tester – rosell.dk Sep 24 '20 at 07:54
1

One more method through exec().

exec('/usr/bin/httpd -M | find "rewrite_module"',$output);

If mod_rewrite is loaded it will return "rewrite_module" in output.

Abhishek Gurjar
  • 7,426
  • 10
  • 37
  • 45
0

Use this function:

function apache_module_exists($module)
{
    return in_array($module, apache_get_modules());
}
Black
  • 18,150
  • 39
  • 158
  • 271
0

For IIS heros and heroins:

No need to look for mod_rewrite. Just install Rewrite 2 module and then import .htaccess files.

Shadi Alnamrouti
  • 11,796
  • 4
  • 56
  • 54
0

Actually, just because a module is loaded, it does not necessarily mean that the directives has been enabled in the directory you are placing the .htaccess. What you probably need is to know: Does rewriting work? The only way to find out for sure is to do an actual test: Put some test files on the server and request it with HTTP.

Good news: I created a library for doing exactly this (detecting various .htaccess capabilities). With this library, all you need to do is this:

require 'vendor/autoload.php';
use HtaccessCapabilityTester\HtaccessCapabilityTester;

$hct = new HtaccessCapabilityTester($baseDir, $baseUrl);
if ($hct->rewriteWorks()) {    
    // rewriting works
}

(instead of $baseDir and $baseUrl, you must provide the path to where the test files are going to be put and a corresponding URL to where they can be reached)

If you just want to know if the module is loaded, you can do the following:

if ($hct->moduleLoaded('rewrite')) {    
    // mod_rewrite is loaded (tested in a real .htaccess by using the "IfModule" directive)
}

The library is available here: https://github.com/rosell-dk/htaccess-capability-tester

rosell.dk
  • 2,228
  • 25
  • 15