How to check whether mod_rewrite is enable on server?

316,780

Solution 1

from the command line, type

sudo a2enmod rewrite

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

Solution 2

  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.

Solution 3

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.

Solution 4

This works on CentOS:

$ sudo httpd -M |grep rewrite_module

Should output rewrite_module (shared)

Solution 5

console:

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

sudo a2enmod rewrite
sudo service apache2 restart
Share:
316,780
knightrider
Author by

knightrider

Updated on September 23, 2021

Comments

  • knightrider
    knightrider over 2 years

    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.

  • Clive
    Clive over 12 years
    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?
  • knightrider
    knightrider over 12 years
    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.
  • Clive
    Clive over 12 years
    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
  • MrWhite
    MrWhite almost 12 years
    apache_get_modules() will only work if PHP is running as an Apache module. The OP states that PHP is running on Lightspeed.
  • MrWhite
    MrWhite almost 12 years
    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.
  • Fernando Sanchiz
    Fernando Sanchiz about 8 years
    I've tried a lot to solve this and then when try this it works for me. I have the last apache and ubuntu now.
  • Greg Noe
    Greg Noe over 7 years
    For the first step I had to enter: <?php echo phpinfo(); ?>
  • Nishant Ghodke
    Nishant Ghodke over 7 years
    Thanks. I owe you a Budweiser. <3
  • Shakeel Ahmed
    Shakeel Ahmed almost 7 years
    Perfect one line solution to check rewrite module enabled or not via PHP codes.
  • Adi Prasetyo
    Adi Prasetyo over 6 years
    i use 7.2, apache_get_modules got undefined. Do i have to restart apache2 after editing .htaccess ?
  • João Pimentel Ferreira
    João Pimentel Ferreira over 6 years
    what does it mean the ... in <Directory ...> ? What shall I put there? the directory where .htaccess is?
  • João Pimentel Ferreira
    João Pimentel Ferreira over 6 years
    yes indeed, the ... should mention between quotes "..." the directory where .htaccess is
  • Rory O'Kane
    Rory O'Kane over 6 years
    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.
  • D. Dan
    D. Dan over 5 years
    It had. I had the conf file copied from the server, and while I had changed the ServerName, I had not changed the directory name, after that and an apache restart it was ok.
  • John T.
    John T. over 5 years
    This is the best way to do it on CentOS, assuming you have ssh access. Thanks, @radtek!
  • Sinthia V
    Sinthia V over 5 years
    RichardIf this answer worked for you and solved your problem you should mark it as correct and award @Jahmic his due.
  • androsfat
    androsfat almost 5 years
    In Debian 9, it does work but you must be logged in as root or use sudo
  • MindRoasterMir
    MindRoasterMir about 4 years
    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
    MindRoasterMir about 4 years
    It should also be kept in mind that the changes take effect only after you have restated your apache. thanks
  • Amit Verma
    Amit Verma almost 4 years
    No you don't need to restart your server when using htaccess.
  • Avatar
    Avatar about 2 years
    sudo: a2enmod: command not found NOT enabled. 🙂 ... Install via sudo a2enmod rewrite and restart server sudo service apache2 restart.
  • Leandro Bardelli
    Leandro Bardelli about 2 years
    will return "Module rewrite already enabled" if it's ok.