Project failure or really a technical error? Microsoft Hero
Feb 17

Two days. Two days of massive fighting with Apache and with .htaccess but I ended up winning this fight (and eventually the battle as well). I will give a simplified version of the problem, it’s enough to understand what was going on.

I was trying to create SEO URLs with the help of Apache’s mod_rewrite and .htaccess. Now consider the following URL:

http://www.example.org/article.php?id=2341

Not the most search engine optimised URL, right? What we would like to create from this looks like this:

http://www.example.org/article/2341/

Here’s a simple diagram how the mod_rewrite works:

Apache mod_rewrite

It’s pretty straigthforward. Firstly we need to create a .htaccess in the folder where we have the examples. If you run Apache server on your machine then create a folder under the www (/var/www under Linux) directory, and name it ’seo’.
To see the folder you will need to enter http://localhost/seo/ into your webbrowser.

Now create a .htaccess file in your seo folder and add the following to it:

1
2
3
RewriteEngine on
RewriteBase /seo
RewriteRule ^articles/([0-9]+)$ articles.php?id=$1

Create another file and name it articles.php and copy the following to it:

1
2
3
4
<?php
$iId = $_GET['id'];
echo 'You have selected article id #' . $iId;
?>

Before running this example don’t forget to enable the mod_rewrite option for Apache. To do this (or to check whether you have this module enabled) open your httpd.conf file and find the following line

1
LoadModule rewrite_module modules/mod_rewrite.so

If there is a #sign before it remove it (uncomment it). Don’t forget to restart your Apache server to enable your settings!

Now go to your browser and open the page http://localhost/seo/articles.php. You should see a screen which says “You have selected article id #”. Change the URL to http://localhost/seo/articles.php?id=2341
Now you should see exactly the same:

article.php with id

And now the moment of truth, enter http://localhost/seo/articles/2341 to your browser’s address bar (no trailing slash!). There are two kind of people now. The ones who see the same output as before, or the ones who see “You have selected article id #”. The ones who get an empty id should read on.

article correctly brining up the id

article not showing id

I did an exhaustive research on this topic, what could go wrong? For some strange reason, when Apache sees /articles/ it automatically loads articles.php. And this why the id is not displayed as only the articles.php gets invoked, without any parameter. I can tell you now that the error is not in .htacccess but in Apache’s configuration file, httpd.conf.

I run AppServ on my machine. I asked a friend of mine to run the previously mentioned scripts, he had no problems running it. I asked him to send over his httpd.conf file. Shockingly, there were differences in my version and in his (he runs WampServer). I simply copied the 2 relevant sections over and the RewriteRule started work like charm.

Open your httpd.conf file and find the following lines (yours might be sligthly different):

1
2
3
4
5
6
7
<Directory />
Options FollowSymLinks ExecCGI Indexes
AllowOverride None
Order deny,allow
Deny from all
Satisfy all
</Directory>

Rewrite this to:

1
2
3
4
5
6
7
<Directory />
Options FollowSymLinks
AllowOverride None
Order deny,allow
allow from all
Satisfy all
</Directory>

Find this in your file (again, yours might be a bit different):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<Directory "C:/AppServ/www">
#
# Possible values for the Options directive are "None", "All",
# or any combination of:
#   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important.  Please see
# http://httpd.apache.org/docs/2.2/mod/core.html#options
# for more information.
#
Options Indexes FollowSymLinks MultiViews ExecCGI
 
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
#   Options FileInfo AuthConfig Limit
#
AllowOverride All
 
#
# Controls who can get stuff from this server.
#
Order allow,deny
Allow from all
 
</Directory>

And rewrite it to:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<Directory "C:/AppServ/www">
#
# Possible values for the Options directive are "None", "All",
# or any combination of:
#   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important.  Please see
# http://httpd.apache.org/docs/2.2/mod/core.html#options
# for more information.
#
Options Indexes FollowSymLinks
 
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
#   Options FileInfo AuthConfig Limit
#
AllowOverride All
 
#
# Controls who can get stuff from this server.
#
Order Deny,Allow
Allow from all
Allow from 127.0.0.1
 
</Directory>

And yet again, don’t forget to restart your Apache server before testing the modifications! This solved the problem for me, I hope you find this solution useful.

7 Responses to “Apache mod_rewrite problem”

  1. Ecommerce Michigan Ecommerce Base Business Home Online Opportunity Says:

    Ecommerce Michigan Ecommerce Base Business Home Online Opportunity…

    I didn’t agree with you first, but last paragraph makes sense for me…

  2. Jason Says:

    Thank You.

    I also beat my head against this for 2 days.

    Jason

  3. Josh Says:

    Many thanks.

  4. viaria Says:

    thanks,
    i think this is solution but i am still getting 500 internal server error on my localhost..

  5. viaria Says:

    sory for the early post
    it is working.. but this mail form is not working..
    i posted apology yesterday when i wrote first.. it does not appear..
    thanks for your article..

  6. Saevar Einarsson Says:

    Thank you, thank you, thank you, i was having this problem in apache 2.2 but now it’s working thank’s to you :D

  7. ilovekhym Says:

    Wow!!! Thank you very much for this tutorial. ^^

Leave a Reply

Powered by WP Hashcash