Skip to main content

How to check mod_rewrite module is enabled using PHP script?


Generally what programmers do is uploading a test.php file to the server. The file will contain  the below code

<?php phpinfo(); ?>



But this won't work if you want to check whether Your webserver's mod_rewrite is enabled or not. mod_rewrite is not a PHP module , it is an extension of your webserver (Apache / IIS ), So  It won't be visible in phpinfo(). In some servers you can see this in Apache info section of listing. It may be visible in the loaded modules section.

This article discuss varies tips to check whether the rewrite module is enabled or not and also it give an idea about how to enable it.



########################
SIMPLE HTACCESS CHECK
#########################

A simple way to check this, if you are using Apache as webserver is:

Upload a .htaccess file in one of the folders in your server and make sure the file contains following line:

RewriteEngine on

Point your browser to the folder. If you get a Server Error, it isn't installed. Otherwise it is.

Example:
Put a .htaccess file to location http://www.yourserver.com/folder/
Now take the url  http://www.yourserver.com/folder in browser . If it shows Server error then mod_rewrite is not installed l



########################
USING PHP SCRIPTS
#########################


Here is the PHP scripts that checks for mod_rewrite on Apache and IIS.

If you're using PHP as an Apache module (called 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());

The function "apache_get_modules" returns

Array
(
   [0] => core
   [1] => http_core
   [2] => mod_so
   [3] => sapi_apache2
   [4] => mod_mime
   [5] => mod_rewrite
)

If you're using PHP as CGI, above script won't work and it will return an error stating "call to undefined function"

In such case you can test it using the following, though

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

If the above evaulates  true, then mod_write is enabled.


Using CGI : a PHP process is launched by Apache, and it is that PHP process that interprets PHP code , not Apache itself
Using PHP as an Apache module (called mod_php) : the PHP interpreter is then kind of "embedded" inside the Apache process : there is no external PHP process -- which means that Apache and PHP can communicate better.



############################################
CHECK USING ENVIRONMENT VARIABLES
############################################


For this create a .htaccess file with following lines  .htaccess

Set an Environment variable "HTTP_MOD_REWRITE"


 // 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


Then, you can check in your PHP code for

    array_key_exists('HTTP_MOD_REWRITE', $_SERVER);

If the script return TRUE, then Module is enabled.


######################################################
SCRIPT TO CHECK MOD_REWRITE MODULE IN APACHE AND IIS
######################################################

To check for Mod_rewrite module in both Apache and IIS use the following script

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;

if( $mod_rewrite ) echo "Mod_rewrite Enabled";





######################################################
HOW TO ENABLE MOD_REWRITE MODULE IN APACHE
######################################################

Open your Apache config file httpd.conf (apache.conf) and uncomment the following lines (remove the hash symbol ("#") from starting of the line

#LoadModule rewrite_module modules/mod_rewrite.so
#AddModule mod_rewrite.c

The first line tells Apache to load the mod_rewrite module and the second line enables the use of it.
After this you restart your Apache, mod_rewrite should be enabled.

Now add changes to your .htaccess file to check whether your rewrite rules are working.



=========


Reference
Apache mod_rewrite extension : http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html

Popular posts from this blog

Strange problem occured while trying to create a CSV file using PHP Script - The file is not seen on FTP but can download using file's absolute path url

Strange problem occured while trying to create a CSV file - The file is not seen on FTP but can download using file's absolute path url Last day I came across a strange problem when I tried to create a csv file on therver using a PHP script. the script was simply writing a given content as a csv file. The file will be created runtime. What happened was, The script executed fine, file handler for new file was created and contents was wrote into the file using fwrite and it returned the number of bytes that was written.

How to get the Query string of a URL using the Javascript (JS)?

JS function get the Query string of a URL or value of each parameter using the Javascript(JS)? If you want to get your current page's url var my_url=document.location; to get the query string part of the url use like this: var my_qry_str= location.search; this will return the part of the url starting from "?" following by query string Lets assume that your current page url is http://www.crozoom.com/2013/page.html?qry1=A&qry2=B then the location.search function will return " ?qry1=A&qry2=B " to exclue "?", do like this:


Urgent Openings for PHP trainees, Andriod / IOS developers and PHP developers in Kochi Trivandrum Calicut and Bangalore. Please Send Your updated resumes to recruit.vo@gmail.com   Read more »
Member
Search This Blog