How to check for mod_rewrite on PHP CGI

apache, mod-rewrite, php

Solution

With PHP CGI there is no easy way to find out whether mod_rewrite is available or not on the server.

So, you need to find out it by making a test call to the server. You can use the following steps for this method,

Create a directory named `mod_rewrite_test` on the server.

Add `.htaccess` file in this directory with the following code,

`<IfModule mod_rewrite.c> RewriteEngine On RewriteBase /mod_rewrite_test RewriteRule .* mod_rewrite_test.txt </IfModule>`

a. first line tells apache to execute the below code only if mod_rewrite is available, b. second line enables rewrite engine, c. third line sets rewrite base directory path which is from `DOCUMENT_ROOT` d. forth line returns the content of `mod_rewrite_test.txt` file to any request sent to `mod_rewrite_test` directory.

Create a file named `mod_rewrite_test.txt` in the same directory. Write `ok` in this file and save it.

Now, using `CURL` in your php script, call the URL similar to,

http://www.your-domain.com/mod_rewrite_test/test.php and check the response.

So, as per our `.htaccess` code, if mod_rewrite is active and working on the server, it will return the content of `mod_rewrite_test.txt` file i.e. `ok` even though the test.php file does not exists.

Otherwise, it will return 404 - Page not found error.

Problem

I am on shared host and PHP is inatalled as CGI script and that is all the problem i am not able to find whether mod_rewrite if enable or not `Note: I don't have any root level access so i can't much do with Shell.` I have tried the following : 1) checked in phpinfo() where i came to know about that this is the wrong place to look for in PHP-CGI. 2) I have tried getting it from apache_get_modules which agi does not work in PHP-CGI :( 3) I have tried : ``` if (strpos(shell_exec('/usr/local/apache/bin/apachectl -l'), 'mod_rewrite') !== false) { // mod_rewrite is enabled } ``` which is asking for path to apache and i dont have this info SHELL cant reply to me and $_SERVER has nothing. 4) I have checked with `RewriteEngine On` in .htaccess and after this my site is throwing 500 Internal server error may be because of RewriteEngine is not there, but i need this is written to show someone. Any body has any idea how to check get this DONE. Thanks

Original source