Getting mod_rewrite to Work on Apache2

Penguin January 23rd, 2007

mod_rewrite is a godsend for any web developer who wants to make pretty URLs. Unfortunately, the documentation to get mod_rewrite working on Apache2 isn’t all that plentiful.

Most of the guides tell you to insert or uncomment the following lines in your httpd.conf file

LoadModule rewrite_module modules/mod_rewrite.so
ClearModuleList
AddModule mod_rewrite.c

If your Apache server was like mine, this will give you a Failed to start error. If you remove the “AddModule mod_rewrite.c” line, then Apache will start again.

The Solution:

Make sure you have the following line uncommented in your http.conf

LoadModule rewrite_module modules/mod_rewrite.so

Next, scroll through your httpd.conf file until you see the following block

Options FollowSymLinks
AllowOverride None

A few lines under that, you should see this line

<Directory “C:/Apache/Apache2/htdocs”>

If this is your test server on your local machine, you can go ahead and just edit the commands within this block. Otherwise, you’ll have to create a new directory block like this

<Directory “C:/Apache/Apache2/htdocs/mydirectory”>

Where “mydirectory” is the directory you want to change the settings for.

Within this block, you’ll find the following

AllowOverride None

What this command does, is enable and disables different directives in your .htaccess file. If you change it to “AllowOverride All”, it will enable everything, especially mod_rewrite.

Once that line is changed, save your httpd.conf file and restart Apache.

You should have a fully funtional Apache with mod_rewrite goodness. In order to test the changes, try putting the following into your .htaccess file. **Warning! DO NOT test this if you are expecting any sort of traffic.

RewriteEngine ON
RewriteRule .* - [F]

Depending on server config you may need either or both of the following:
AllowOverride FileInfo
Options +FollowSymLinks

So, your final file may look like this:
AllowOverride FileInfo
Options +FollowSymLinks
RewriteEngine ON
RewriteRule .* - [F]

This code will return a “Forbidden” page for every address you put in.

Happy coding!

Related posts

Trackbacks

close Reblog this comment
blog comments powered by Disqus