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!
This question is related to
php
apache
mod-rewrite
iis
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.
<?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 />"; ?>
Two lines of code:
$isEnabled = in_array('mod_rewrite', apache_get_modules());
echo ($isEnabled) ? 'Enabled' : 'Not enabled';
<?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>
Use this function:
function apache_module_exists($module)
{
return in_array($module, apache_get_modules());
}
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.
For IIS heros and heroins:
No need to look for mod_rewrite. Just install Rewrite 2 module and then import .htaccess files.
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.
don't make it so difficult you can simply find in phpinfo();
Hope helpful!
Thanks
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.
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
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.
via command line we in centOs we can do this
httpd -l
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.
Source: Stackoverflow.com