mod rewrite - Fake Directories

Let's see how good you are at picking out the fake directories on this website. Using Apache's powerful mod rewrite, most of the directories I create here on this website are fake! If you look at the list of the sections of this website, they would appear something like this:

Using Apache's mod rewrite to fake directories

Let's say you had a script on your website, and you name it : category.php. All this page does is to generate category 'introductions' for your website. Assume you had to generate the following three categories dynamically, as values, in a query string:

  1. html - /category.php?cat=html
  2. css - /category.php?cat=css
  3. php - /category.php?cat=php

and you decide you want them to appear on your website as:

  1. /html/
  2. /css/
  3. /php/

Sample mod rewrite code

Create a .htaccess file if one does not already exist in your WWW root. Insert the following code:

code:
RewriteEngine on
RewriteRule ^([^/]+)/$	category.php?cat=$1 [L]

save the file and upload it. Now everytime you type in the url: http://www.example.com/html/ you will see the page that is actually http://www.example.com/category.php?cat=html

How cool is that?

Faking directories with mod rewrite; 1 slight problem to fix...

This will be alright until one day you decide to actually add a real sub-directory to your website e.g. /forum/ or try to access some other existing folder / directory like /cgi-bin/

What happens now? :eek:

Just bring up the .htaccess file again and add the following lines to it and your file should now look a bit like this:

code:
RewriteEngine on

RewriteCond %{REQUEST_FILENAME}  -d
RewriteRule  ^.*$  -  [L]

RewriteRule ^([^/]+)/$	category.php?cat=$1 [L]

TOP

Do you like the new design?

 Yes

 No

 Don't know