How to find if mod_rewrite is enabled or not using php

I would like to know if Apache’s mod_rewrite module is enabled or not using PHP.
Case 1:
If PHP is run as an Apache module then its easy to find the loaded module using phpinfo under:
apache2handler >> Loaded Modules >> search for mod_rewrite

Case 2:
If PHP is run as CGI module then there is no such info in phpinfo.
In this case how to find if mod_rewrite module is enabled or not using php.

Thanks in advance.

You need to actually test it. Set up a simple RewriteRule and 2 files. For example:

RewriteRule file1\.txt file2.txt

Then read the file:

$file = file_get_contents(‘http://yourhost.com/file1.txt’);

If you get contents of file 1, mod_rewrite is not there.

I need purely using PHP only.
In this case you are using .htaccess.

If you want to test via .htaccess, it’s simple because the server gives the ‘500 internal server error’ in case of mod_rewrite disabled.

has anybody some idea?
Thanks

Aternative for case 1 (an easier way):

Case 1:
If PHP is run as an Apache module then its easy to find the loaded module using phpinfo under:
apache2handler >> Loaded Modules >> search for mod_rewrite

<pre>
<?php
print_r(apache_get_modules());
?>
</pre>

search for mod_rewrite…

more about apache_get_modules

is there no solution for Case 2 :shifty:

I doubt that PHP as a CGI module would have permission to find out if mod_rewrite is enabled.

is it possible by some means of exec()?

Someone who has PHP set up as a CGI module might be able to help there.

How could he help? via shell command or else?

If shell access is enabled, you could try something like…

[root@localhost ~]# [B]/usr/local/apache/bin/apachectl -l[/B]
Compiled in modules:
  core.c
  mod_authn_file.c
  mod_authn_default.c
  mod_authz_host.c
  mod_authz_groupfile.c
  mod_authz_user.c
  mod_authz_default.c
  mod_auth_basic.c
  mod_include.c
  mod_filter.c
  mod_log_config.c
  mod_env.c
  mod_setenvif.c
  prefork.c
  http_core.c
  mod_mime.c
  mod_status.c
  mod_autoindex.c
  mod_asis.c
  mod_cgi.c
  mod_negotiation.c
  mod_dir.c
  mod_actions.c
  mod_userdir.c
  mod_alias.c
  mod_rewrite.c
  mod_so.c

Then just check to see if ‘mod_rewrite’ is in that response:

if (strpos(shell_exec('/usr/local/apache/bin/apachectl -l'), 'mod_rewrite') !== false) {
    // mod_rewrite is enabled
} else {
    // not enabled
}

/usr/local/apache/bin/apachectl -l

How to know the location of apache bin?

There whereis command can let you know that.


# whereis apache
apache: /usr/local/apache /usr/local/apache.backup /usr/local/apache.backup_archive

Thanks pmw57.
command noted.