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!

15 Answers 15

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.

Sign up to request clarification or add additional context in comments.

11 Comments

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.
That's true, so there is properly no general script?
@DrCord Then I'm guessing you're using it in CGI.
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...
@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.
|
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> 

2 Comments

Will phpinfo also tell me if I don't have mod_php enabled?
yes we can see in phpinfo.php all Apche's "Loaded Modules"
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.

6 Comments

With a <IfModule mod_env.c> it would be almost perfect. :)
This is a very useful and simple solution and it works even if PHP is running as CGI
as @RibeiroBreno mentioned, with <IfModule mod_env.c> it would be almost perfect. this worked for me perfectly.
If PHP is running as CGI, this is the way to go. Works beautifully.
I like this idea. Another advantage : this will indirectly also check if the .htaccess was taken into account.
|
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.

Comments

28

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

enter image description here

Hope helpful!

Thanks

2 Comments

This was the quickest and simplest route for me, since I was already doing stuff with a phpinfo call/file. Thanks! :)
@loyola , I can't update my comment, so I deleted, thank
11

via command line we in centOs we can do this

httpd -l 

4 Comments

And if you are on ubuntu: apache2 -l
Or apachectl -M in Ubuntu as well
This seems to show only modules that were compiled in.
httpd -M shows list of loaded Static and Shared modules.
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 />"; ?> 

Comments

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.

Comments

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.

1 Comment

@kba How to check for then in CGI mode.
3

Two lines of code:

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

Comments

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.

1 Comment

I created a library that builds on this exact idea: github.com/rosell-dk/htaccess-capability-tester
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.

Comments

0

Use this function:

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

Comments

0

For IIS heros and heroins:

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

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.