Monday, 17 March 2014

How to enable mod_rewrite in Apache2 on Debian7


I have Apache2 installed as my web-server. It appears to work. So what is this mod_rewrite thing and why do I need it?

The point of the web server is to serve up web pages; mostly these days, the content of those web pages comes from various content management systems such as Drupal, Joomla, Wordpress and the like. 

They do a fine job of storing and organising content but not alays such a good job of serving it up under decent page addresses (URL's). Which is where the mod_rewrite module comes in...


The Apache2 web server is designed with pluggable modules in mind to extend the base functions. One such module is mod_rewrite which provides for rewriting website URLs at the server side. In this way you can forget about clunky and un-typeable page addresses such as http://myserver.com/post.php?category=100&post=200 in favour of  the much easier http://myserver.com/my_category/my_post.html; the translation of which can be handled by the server.


mod_rewrite improves the usability and search engine friendliness of web sites by exposing more memorable and crawlable URL's.Without it, Wordpress and Joomla become unwieldy beasts to say the least. As a side effect, it can aid site security by hiding sensitive information such as query strings from URL requests.

The good news is if you installed the Apache2 web server via apt-get or aptitude on Debian or Ubuntu systems, the mod_rewrite module is automatically  installed.  But not enabled, which it the bad news. After the Apache2 installation, you need to enable mod_rewrite.

Enable mod_rewrite on Apache2

To verify that mod-rewrite is indeed installed to /etc/apache2/mods-available/rewrite.load, the command:
$ cat /etc/apache2/mods-available/rewrite.load

That should return:
LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so

It just needs to be enabled. The command:

$ sudo a2enmod rewrite

will create a symbolic link in /etc/apache2/mods-enabled, which you can check using:
$ ls -al /etc/apache2/mods-enabled/rewrite.load

which should output something like:
lrwxrwxrwx 1 root root 30 Dec  9 23:10   /etc/apache2/mods-enabled/rewrite.load -> ../mods-available/rewrite.load

There's a setting which needs editing in the Apache2 defaults:
$ sudo gedit /etc/apache2/sites-available/default
Replace every occurrence of "AllowOverride None" with "AllowOverride all".

Finally, restart Apache2 to apply:
/etc/init.d/apache2 reload

RC

No comments:

Post a Comment

At least try to be nice, it won't kill you...