Home  »     »     »     »     »  How to check mod_rewrite module is enabled using PHP script?

How to check mod_rewrite module is enabled using PHP script?

Wednesday, July 16, 2014


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




How to link to this page?
If you wish to link to this page from your website, simply Copy and paste the above HTML code to your web page. It will appear on your page as:
How to check mod_rewrite module is enabled using PHP script?.




Share this!




comments powered by Disqus

This Weeks 7 Popular Posts


Subscribe to Recent Posts by Email
Stay connected to CROZOOM with regular Email notices of new Techie articles and IT Jobs. Updates will be delivered to your Inbox as soon as they are posted online.

Enter Your Email Address:  

Delivered by FeedBurner   RSS Feed

Search this Blog   



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 »