How to check if mod_rewrite is enabled in php?

PhpApacheMod RewriteIis

Php Problem Overview


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!

Php Solutions


Solution 1 - Php

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.

Solution 2 - Php

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>

Solution 3 - Php

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.

Solution 4 - Php

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.

Solution 5 - Php

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

enter image description here

Hope helpful!

Thanks

Solution 6 - Php

via command line we in centOs we can do this

httpd -l

Solution 7 - Php

<?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 />";  ?>

Solution 8 - Php

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.

Solution 9 - Php

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.

Solution 10 - Php

Two lines of code:

$isEnabled = in_array('mod_rewrite', apache_get_modules());
echo ($isEnabled) ? 'Enabled' : 'Not enabled';

Solution 11 - Php

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.

Solution 12 - Php

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.

Solution 13 - Php

Use this function:

function apache_module_exists($module)
{
    return in_array($module, apache_get_modules());
}

Solution 14 - Php

For IIS heros and heroins:

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

Solution 15 - Php

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTiesView Question on Stackoverflow
Solution 1 - PhpkbaView Answer on Stackoverflow
Solution 2 - PhpShankar Narayana DamodaranView Answer on Stackoverflow
Solution 3 - Phpa.l.eView Answer on Stackoverflow
Solution 4 - PhpJohnny JensenView Answer on Stackoverflow
Solution 5 - PhployolaView Answer on Stackoverflow
Solution 6 - Phph0mayunView Answer on Stackoverflow
Solution 7 - Phpuser1649798View Answer on Stackoverflow
Solution 8 - PhpAhmedul Haque AbidView Answer on Stackoverflow
Solution 9 - PhpTJHeuvelView Answer on Stackoverflow
Solution 10 - PhpAmal MuraliView Answer on Stackoverflow
Solution 11 - PhpwebfanView Answer on Stackoverflow
Solution 12 - PhpAbhishek GurjarView Answer on Stackoverflow
Solution 13 - PhpBlackView Answer on Stackoverflow
Solution 14 - PhpShadi NamroutiView Answer on Stackoverflow
Solution 15 - Phprosell.dkView Answer on Stackoverflow