[php] How to check whether mod_rewrite is enable on server?

Currently I am using the hosting with lightspeed server. Hosting says mod_rewrite is enabled but I can't get my script working there. Whenever I try to access the URL, it returns 404 - not found page.

I put the same codes at another server which is running with Apache. It's working over there. So I guess, it's the .htaccess and mod_rewrite issue.

But Hosting support is still insisting with me that their mod_rewrite is on, so I would like to know how can I check whether it's actually enabled or not.

I tried to check with phpinfo(), but no luck, I can't find mod_rewrite there, is it because they are using lightspeed?

Is there any way to check? Please help me out. Thank you.

FYI: my .htaccess code is

Options -Indexes

<IfModule mod_rewrite.c>
DirectoryIndex index.php
RewriteEngine on

RewriteCond $1 !^(index\.php|assets|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
</IfModule>

I tried like this also

DirectoryIndex index.php
RewriteEngine on

RewriteCond $1 !^(index\.php|assets|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]

But same result.

This question is related to php .htaccess mod-rewrite url-rewriting lightspeed

The answer is


If this code is in your .htaccess file (without the check for mod_rewrite.c)

DirectoryIndex index.php
RewriteEngine on

RewriteCond $1 !^(index\.php|assets|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]

and you can visit any page on your site with getting a 500 server error I think it's safe to say mod rewrite is switched on.


If apache_get_modules() is not recognized or no info about this module in phpinfo(); try to test mod rewrite by adding those lines in your .htaccess file:

RewriteEngine On
RewriteRule ^.*$ mod_rewrite.php

And mod_rewrite.php:

<?php echo "Mod_rewrite is activated!"; ?>

I was having the exact problem, I solved it by clicking custom structure, then adding /index.php/%postname%/ and it works

Hope this saves someone the stress that I went through finding what the heck was wrong with it.


console:

<VirtualHost *:80>
        ...
    <Directory ...>
        AllowOverride All
    </Directory>
</VirtualHost>

sudo a2enmod rewrite
sudo service apache2 restart

If you are in linux system, you can check all enable modules for apache2(in my case) in the following folder:/etc/apache2/mods-available

cd /etc/apache2/mods-available

to type: ll -a
if you want to check the available modules for php (in this case php 7 ) folder /etc/php/7.0/mods-available

cd /etc/php/7.0/mods-available

to type: ll -a


PHP's perdefined apache_get_modules() function returns a list of enabled modules. To check if mod_rewrite is enabled , you can run the following script on your server :

<?php
print_r(apache_get_modules());
?>

If the above example fails, you can verify mod-rewrite using your .htaccess file.

Create an htaccess file in the document root and add the following rewriteRule :

RewriteEngine on

RewriteRule ^helloWorld/?$ /index.php [NC,L]

Now visit http://example.com/HelloWorld , You will be internally forwarded to /index.php page of your site. Otherwise, if mod-rewrite is disabled , you will get a 500 Internel server error.

Hope this helps.


You can use php function

      apache_get_modules

and check for mod_rewrite

<pre>
<?php
print_r(apache_get_modules());
?>
</pre>

http://in2.php.net/apache_get_modules


This works on CentOS:

$ sudo httpd -M |grep rewrite_module

Should output rewrite_module (shared)


If

in_array('mod_rewrite', apache_get_modules())

returns true then mod-rewrite is enabled.


I know this question is old but if you can edit your Apache configuration file to AllowOverride All from AllowOverride None

<Directory "${SRVROOT}/htdocs">
    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
    # Note that "MultiViews" must be named *explicitly* --- "Options All"
    # doesn't give it to you.
    #
    # The Options directive is both complicated and important.  Please see
    # http://httpd.apache.org/docs/2.4/mod/core.html#options
    # for more information.
    #
    Options Indexes FollowSymLinks

    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   AllowOverride FileInfo AuthConfig Limit
    #
    AllowOverride All

    #
    # Controls who can get stuff from this server.
    #
    Require all granted
</Directory>

If you are using a virtual hosts configuration file, make sure the virtual host in question has the directive AllowOverride All somewhere like this:

<VirtualHost *:80>
        ...
    <Directory "directory/of/your/.htaccess">
        AllowOverride All
    </Directory>
</VirtualHost>

Basically, this states to allow processing of all .htaccess directives.


just make a new page and add this code

 <?php
 if(!function_exists('apache_get_modules') ){ phpinfo(); exit; }
 $res = 'Module Unavailable';
 if(in_array('mod_rewrite',apache_get_modules())) 
 $res = 'Module Available';
?>
<html>
<head>
<title>A mod_rewrite availability check !</title></head>
<body>
<p><?php echo apache_get_version(),"</p><p>mod_rewrite $res"; ?></p>
</body>
</html>

and run this page then find will able to know module is Available or not if not then you can ask to your hosting or if you want to enable it in local machine then check this youtube step by step tutorial related to enable rewrite module in wamp apache https://youtu.be/xIspOX9FuVU?t=1m43s Wamp server icon -> Apache -> Apache Modules and check the rewrite module option


You just need to check whether the file is there, by typing

cat /etc/apache2/mods-available/rewrite.load

The result line may not be commented starting with #


  1. To check if mod_rewrite module is enabled, create a new php file in your root folder of your WAMP server. Enter the following

    phpinfo();

  2. Access your created file from your browser.

  3. CtrlF to open a search. Search for 'mod_rewrite'. If it is enabled you see it as 'Loaded Modules'

  4. If not, open httpd.conf (Apache Config file) and look for the following line.

    #LoadModule rewrite_module modules/mod_rewrite.so

  5. Remove the pound ('#') sign at the start and save the this file.

  6. Restart your apache server.

  7. Access the same php file in your browser.

  8. Search for 'mod_rewrite' again. You should be able to find it now.


you can do it on terminal, either:

apachectl -M
apache2ctl -M

taken from 2daygeek


This code worked for me:

if (strpos(shell_exec('/usr/local/apache/bin/apachectl -l'), 'mod_rewrite') !== false) echo "mod_rewrite enabled";
else echo "mod_rewrite disabled";

from the command line, type

sudo a2enmod rewrite

if the rewrite mode is already enabled, it will tell you so!


Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to .htaccess

Use .htaccess to redirect HTTP to HTTPs Getting a 500 Internal Server Error on Laravel 5+ Ubuntu 14.04 Server unable to read htaccess file, denying access to be safe Laravel 5 – Remove Public from URL Laravel 5 not finding css files How can I fix the 'Missing Cross-Origin Resource Sharing (CORS) Response Header' webfont issue? How Can I Remove “public/index.php” in the URL Generated Laravel? Apache 2.4 - Request exceeded the limit of 10 internal redirects due to probable configuration error Forbidden You don't have permission to access / on this server Htaccess: add/remove trailing slash from URL

Examples related to mod-rewrite

Getting a 500 Internal Server Error on Laravel 5+ Ubuntu 14.04 Forbidden You don't have permission to access / on this server Htaccess: add/remove trailing slash from URL Permission denied: /var/www/abc/.htaccess pcfg_openfile: unable to check htaccess file, ensure it is readable? 404 Not Found The requested URL was not found on this server Redirect all to index.php using htaccess .htaccess not working on localhost with XAMPP URL rewriting with PHP No input file specified htaccess redirect to https://www

Examples related to url-rewriting

Apache 2.4 - Request exceeded the limit of 10 internal redirects due to probable configuration error Rewrite URL after redirecting 404 error htaccess How to change the URL from "localhost" to something else, on a local system using wampserver? .htaccess not working on localhost with XAMPP URL rewriting with PHP Rewrite all requests to index.php with nginx How do I configure IIS for URL Rewriting an AngularJS application in HTML5 mode? .htaccess rewrite subdomain to directory How to remove index.php from URLs? How to redirect a url in NGINX

Examples related to lightspeed

How to check whether mod_rewrite is enable on server?