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

How to apply for a new ration card and what are the documents required?

List of documents required for new ration card application in Kerala and how to apply


Application for a new ration card should be addressed to Taluk Supply Officer (TSO) / City Rationing Officer (CRO) of applicant's residing area.

Primary document required are Residence certificate, Income certificate and incase the applicant's name is in another ration card then he/she should submit the reduction certificate ontained from previous TSO / CRO as proof for removing his/her name from old Ration card.

You can get the new application from your currently residing Taluk Supply Office.

Payback Points - How to redeem - How to merge multiple payback accounts - Block Payback card - Payback customer care

Joomla and Forum Integration - Integrating Forums to Joomla

Joomla is one of the most popular CMS opensource packages. It is very easier to develop website's using Joomla. You just need to download Joomla package from Joomla's Official website www.joomla.org  and install it on your domain and later adding customizations to templates and feature and Your website is ready :). Now a days most websites provides a forum section for it users for discussing various article topics, gathering opinions etc.



Following are some best know forum opensource packages which can be integrated with Joomla and create a new forum experience for users


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