134

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.

Marc.2377
  • 7,807
  • 7
  • 51
  • 95
knightrider
  • 2,533
  • 7
  • 30
  • 42
  • 1
    The simplest way to verify whether `mod-rewrite` is enabled is paste some random texts in your htaccess and then visit your site , you will get an internal server error if the module is enabled. Here is a quick `htaccess tutorial for beginners` I wrote a few months ago, https://helponnet.com/2021/04/15/htaccess-tutorial-for-beginers/ hope you will find it helpful. – Amit Verma Jan 27 '22 at 19:59

19 Answers19

143

from the command line, type

sudo a2enmod rewrite

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

KawaiKx
  • 9,558
  • 19
  • 72
  • 111
115
  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.

Frank N
  • 9,625
  • 4
  • 80
  • 110
Fadzly Othman
  • 1,702
  • 1
  • 13
  • 6
  • 9
    I believe phpinfo() reports the _Apache_ Loaded Modules when PHP is itself being run as an Apache module. The OP states that PHP is running on Lightspeed, which has its own mod_rewrite compatible rewrite engine. – MrWhite Jun 13 '12 at 00:59
  • 3
    For the first step I had to enter: – Greg Noe Nov 17 '16 at 19:53
  • 9
    Note that this method will not work if you are running PHP as a CGI application (which is the case if `phpinfo()`’s “Server API” field shows “CGI/FastCGI”). `phpinfo()` won’t list the enabled modules. In that case, see [How to check for mod_rewrite on PHP CGI](https://stackoverflow.com/q/18310183/578288). – Rory O'Kane Jan 28 '18 at 04:04
94

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.

James John McGuire 'Jahmic'
  • 11,728
  • 11
  • 67
  • 78
26

This works on CentOS:

$ sudo httpd -M |grep rewrite_module

Should output rewrite_module (shared)

radtek
  • 34,210
  • 11
  • 144
  • 111
19

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!"; ?>
wappy
  • 470
  • 3
  • 12
18

console:

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

sudo a2enmod rewrite
sudo service apache2 restart
Lukas Lukac
  • 7,766
  • 10
  • 65
  • 75
16

If

in_array('mod_rewrite', apache_get_modules())

returns true then mod-rewrite is enabled.

ProgramFOX
  • 6,131
  • 11
  • 45
  • 51
Shivanshu
  • 1,230
  • 1
  • 11
  • 17
16

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.

Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50
Amit Verma
  • 40,709
  • 21
  • 93
  • 115
15

you can do it on terminal, either:

apachectl -M
apache2ctl -M

taken from 2daygeek

Adi Prasetyo
  • 1,006
  • 1
  • 15
  • 41
10

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.

Clive
  • 36,918
  • 8
  • 87
  • 113
  • 3
    So you can access some pages on the site without a 500 error? That would generally mean if the .htaccess file is being read that mod_rewrite must be switched on. However the .htaccess might not be being read...try writing some nonsense characters at the top of your .htaccess file, this will cause the connection to die with a 500 server error if Apache is actually reading the file. If it is being read, could you give an example of a URL you think should work but doesn't? – Clive Sep 07 '11 at 17:29
  • the problem is, it's not apache server, it's lightspeed. I never get 500 error, all i get is 404 error. That's why I am doubting that .htaccess is not enable. – knightrider Sep 07 '11 at 17:39
  • Most likely yes, I'd try putting the nonsense characters in to .htaccess and see if you get the 500 error when you go directly to a non-rewritten page (e.g. index.php). If not, it might be worth posting a new question asking how to enable .htaccess with Lightspeed – Clive Sep 07 '11 at 17:47
5

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

Miqdad Ali
  • 6,129
  • 7
  • 31
  • 50
  • 2
    apache_get_modules() will only work if PHP is running as an Apache module. The OP states that PHP is running on Lightspeed. – MrWhite Jun 13 '12 at 00:57
5

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

5

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>
Excellent Lawrence
  • 946
  • 11
  • 11
  • It is a good idea to tell the exact name and when possible the location of the file that need to be changed. It will be helpful for the newcomers. Thanks – MindRoasterMir Apr 17 '20 at 17:51
  • It should also be kept in mind that the changes take effect only after you have restated your apache. thanks – MindRoasterMir Apr 17 '20 at 17:53
4

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

Hassan Saeed
  • 6,326
  • 1
  • 39
  • 37
4

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";
3

First you check if the module is installed

apachectl -M

It it does not shows on the list, you must activate it:

a2enmod rewrite

Now, restart your server and test

systemctl restart apache2
Filipe
  • 73
  • 6
1

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 #

João Pimentel Ferreira
  • 14,289
  • 10
  • 80
  • 109
1

Simply create a php file in root and add following code

<?php
if (in_array('mod_rewrite', apache_get_modules())) {
    echo "mod_rewrite is enabled";
} else {
    echo "mod_rewrite is not enabled";
}
Aleem Iqbal
  • 55
  • 1
  • 10
0

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.

Kyle S
  • 13
  • 5