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.

How to add "Link to this page" option under blogger posts?

Steps in adding Link to this page to your blogger posts Links to your page can improve your page rank. So it is a good option to add HTML code for linking to your web page. So that reader can copy and paste it on their web page. if another website links to your web page, this is considered an external link to your website. External links to your website are the most important source of ranking power and in SEO terminology it is considered as third party ranking vote for your page.

Intex Aqua 5.5 VR Plus genuine Review - Dont Buy Intex Aqua 5.5 VR Plus - Board complaint and low battery backup issues

Intex Aqua 5.5 VR Plus  Review - Dont Buy Intex Aqua 5.5 VR + - Board complaint and low battery backup issues I bought an Intex Aqua 5.5 VR Plus on April 23, 2018, With in a week it started to show Battery backup issues. Even if it is charged full, it will completely drain out with in 12 or 13 hours. During this time No internet was used, only 2 or 3 calls were done. Some times there was issues with net connection also.


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