Sunday, March 16, 2014

How to enable mod_rewrite for Apache web server

I've installed two Content Management Systems on my Debian wheezy system - WordPress and Drupal. The web server deployed is the venerable Apache. Common to the 2 deployments is the issue of how to enable the mod_rewrite module for the Apache web server.

The rewrite module maps obscure URLs - computer gibberish - to 'clean' URLs. The default URLs, for both WordPress and Drupal, are something like http://localhost/wordpress/?p=248.

You can configure the URLs to be rewritten to resemble the post titles: http://localhost/wordpress/a-board-game/. However, the change won't take effect unless the mod_rewrite module is enabled.

Query module status

To check if mod_rewrite is currently enabled:

  1. Create a PHP script to list the enabled Apache modules, and place it in the Document Root directory (by default: /var/www):
    $ cat > /var/www/testmod.php
    <?php
    print_r(apache_get_modules());
    ?>
    
  2. Run the script by entering http://localhost/testmod.php into a browser.

If the mod_rewrite module is enabled, you will find it in the output of the PHP script.

Enable rewrite

To enable:

  1. Enable mod_rewrite module.
    $ sudo a2enmod rewrite
    Enabling module rewrite.
    To activate the new configuration, you need to run:
      service apache2 restart
    
  2. Set AllowOverride to All in Apache configuration files.

    Apache configuration files contain directives to control web server behavior. The configuration directive we're interested in is AllowOverride. For Debian wheezy, look for the directive in /etc/apache2/sites-available/default.

    $ more /etc/apache2/sites-available/default
        ..snipped..
     <?Directory /var/www/>
      Options Indexes FollowSymLinks MultiViews
      AllowOverride None
      Order allow,deny
      allow from all
     <?/Directory>
        ..snipped..
    

    When the AllowOverride directive is set to None for a directory, then Apache ignores all .htaccess files in that directory and in its sub-directories.

    The .htaccess files are 'directory-level' Apache configuration files. Web applications such as WordPress and Drupal have a .htaccess file in their respective web directories.

    In the above example, AllowOverride is set to None for /var/www, the Document Root directory. Therefore, all .htaccess files are ignored in the entire web hierarchy.

    Modify AllowOverride to have value All.

     <?Directory /var/www/>
      Options Indexes FollowSymLinks MultiViews
      AllowOverride All
      Order allow,deny
      allow from all
     <?/Directory>
    
  3. Restart Apache.
    $ sudo service apache2 restart
    [ ok ] Restarting web server: apache2 ... waiting .
    

Disable rewrite

To disable:

$ sudo a2dismod rewrite
Module rewrite disabled.
To activate the new configuration, you need to run:
  service apache2 restart
$ service apache2 restart
[ ok ] Restarting web server: apache2 ... waiting .

3 comments:

Anonymous said...

In the above example, AllowOverride is set to None for /var/wwww, the Document Root directory.

One less w would be more betterer. (/var/www)

the $ prompt implies running as normal user. These commands require running as root so $ sudo ... or # ... would be more betterer.

Nice article

Peter Leung said...

Good catches. Article was updated with your suggestions. Thanks

Riyan Widiyanto said...

Thank you, this article helped me.