.htaccess mod_rewrite Tutorial


Want a cool site? Want it to look purty? Here’s how.

Let’s say you already have the site. catolog.php has the catolog, item.php?id=1 has the item viewer, and checkout.php?id=1 lets you check out. I will be showing you a script to pretty up these URLs.


RewriteEngine on
RewriteRule ^catalog/?$ /catalog.php
RewriteRule ^item/([0-9]+)/?$ /item.php?id=$1
RewriteRule ^checkout/([0-9]+)/?$ /checkout.php?id=$1

This makes pretty URLs using .htaccess. Explanation of the item thing. First, it defines item/ as a valid virtual directory. Then, ([0-9]+) tells it to save any numerical value as $1. Finally, /? means it’ll work with or without an ending slash. Now, it transparently redirects to item.php?id=$1 where $1 is the item number.

Hope this helps!


This looks really, really nice. It’s something that I could honestly make use of.

Just a minor slip though. I think you meant “view.php?id=1” when you said “item.php?id=1”


Whoops. Thanks for pointing that out, dev!


where can i find .htaccess ? sorry completely newbie.


You have to create it. Just create a file called .htaccess and save it in the public_html folder.


.htaccess can be found in your root directory (i.e. the public_html folder). If you cannot see it, you can create one by typing out the text in a text editor (e.g. Notepad) and saving the file as whatever you like. Upload the file to the public_html folder and use the Rename option in the file manager to change the file name to .htaccess

Windows will not normally allow you to name files on your own computer as just an extension, but there are ways around it such as using “fopen(’.htaccess’, ‘w’);” in PHP.


I have written following rules for my website:
#Redirect domain.com to www.domain.com
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www.wallads.info
RewriteRule (.*) http://www.wallads.info/$1

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^page/([^/.]+)/?$ index.php?page=$1

But still it is showing default link : http://www.wallads.info/index.php?page=2

I think this rule is not working properly, or I need to modify it?


Okay guys,
Here are two extra codes for a little bit more security.

Deny access to all your htaccess files.
Because only the site owner needs to be able to change them.

<Files .htaccess>
order allow,deny
deny from all

Ever seen a directory without a index? Well who has not?
You get a white page with a headline named: index of … something.
With a list of files from your folder.
Now how do we block user to see those files?
Easy! Use this code in your htaccess file:

IndexIgnore *

You can of course just use a blank html doc for this.
But when your using tons of folders inside your domain this rule will be great!


Got same problem :frowning: