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:
- http://www.desilva.biz/forum/
- http://www.desilva.biz/grafix/
- http://www.desilva.biz/hw/
- http://www.desilva.biz/misc/
- http://www.desilva.biz/webdsn/
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:
- html - /category.php?cat=html
- css - /category.php?cat=css
- php - /category.php?cat=php
and you decide you want them to appear on your website as:
- /html/
- /css/
- /php/
Sample mod rewrite code
Create a .htaccess file if one does not already exist in your WWW root. Insert the following 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? ![]()
Just bring up the .htaccess file again and add the following lines to it and your file should now look a bit like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [L]
RewriteRule ^([^/]+)/$ category.php?cat=$1 [L]
