How to enable mod_rewrite in Apache 2.2

The mod_rewrite is a module for Apache Web Server which allows you to rewrite and manipulate URLs which are sent to your webserver. In this tutorial I will explain how to enable mod_rewrite with Apache 2.2* in a Debian Linux.

To enable the module, type this command:

a2enmod rewrite

Output:

root@server:~# a2enmod rewrite
Module rewrite installed; run /etc/init.d/apache2 force-reload to enable.
root@server:~#

Now reload apache:

/etc/init.d/apache2 force-reload

Output:

root@server:~# /etc/init.d/apache2 force-reload
Forcing reload of web server (apache2)…httpd (1654)
root@server:~#

All went fine, but is possible that you receive an error, for example, if you have some defined mod_rewrite rules setted in the apache.conf or httpd.conf you can get this error as output:

root@server:~# /etc/init.d/apache2 force-reload
Forcing reload of web server (apache2)… waiting Syntax error on line 123 of /etc/httpd/apache2.conf:
RewriteBase: only valid in per-directory config files failed!
root@server:~#

To fix this kind of error is pretty simple, you need only to remove the rule from apache2.conf, in my example I removed this code:

1
2
3
4
5
6
7
IfModule mod_rewrite.c<br />
RewriteEngine On<br />
RewriteBase /test/<br />
RewriteCond %{REQUEST_SCRIPT} !-f<br />
RewriteCond %{REQUEST_SCRIPT} !-d<br />
RewriteRule . /test/index.php [L]<br />
/IfModule

Now you need to put the above code in the correct place, inside a folder. In my example code I need to modify URLs of all requests made under the folder /test/, so I will copy the code in an empty file named .htaccess and I will save the file under /test/.htaccess.

Now we need to set the option AllowOverride in our apache2.conf for the folder /test/ and I will use vi to edit the apache2 config file:

root@server:~# vi /etc/httpd/apache2.conf

Add this code:

1
2
3
4
5
6
	Directory /var/webhost/yoursite.com/public_html/test/<br />
		Options Indexes FollowSymLinks MultiViews<br />
		<b>AllowOverride All</b><br />
		Order allow,deny<br />
		allow from all<br />
	/Directory

Note that I used the option AllowOverride to All. To see if all is working good, just reload your apache:

/etc/init.d/apache2 reload

and test the changes in your browser.

Random Posts

Previous Posts