How to enable mod_rewrite for Apache web server
I've installed two Content Management Systemson 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_rewritemodule 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:
- 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
print_r(apache_get_modules());
?> - 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:
- Enable mod_rewrite module.
$ sudo a2enmod rewrite
Enabling module rewrite.
To activate the new configuration, you need to run:
service apache2 restart - 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..
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
..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.
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
- 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 .