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 delete videos from your Youtube Watch History list?

How to Delete Individual or all videos from your Youtube Watch History list? Youtube keeps a fine record of the videos that you had watched earlier. You can view this by visiting the History section. If you want to remove the video's from the list do the following: Logon to Youtube and click on the "History" tab on the left menu to view Watch History ( Read more ) There will be check boxes corresponding to each video in the list Tick the check boxes of the videos which you want to remove Click on " Remove " button to delete the videos.

ICICI prudential Customer portal updated - Option to change password is missing - Know how to change your ICICI prudential password

Recently I received an SMS from ICICI prudential asking for login to their website's customer portal using the phone number as user Id and an autogenerated one time password given in the message as password. The SMS messsage was like this. Dear ***Cust Name*** login to your policy(ies) on www.iciciprulife.com with your user id as **mobile number*** and One time use password as ***password***

What are the Income Tax Rates for Indian citizens for Financial Year 2017-2018?

Income Tax Slab and Rates given below are for Indian citizens of age less than 60. This rates are applicable for the Financial Year 2017-2018 Income Tax Slab Rates Financial Year 2017-2018 Assessment Year 2018-19 Income Tax Slab Rates SLAB 1 Individuals whose total income not exceeding Rs. 2,50,000 ( 2.5 lakhs ) They are exempted from paying income tax.


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