I've been asked a few times about how mod_rewrite rules are done, so I thought that I would do a brief "primer" on how it works and some examples on how to implement it. Mod_rewrite'd URL's are a commonly used method of transforming url's into something like http://example.com/category/article-description/
Benefits:
How it works:
The mod_rewrite engine sits between the address typed in, and the "webserver". It intercepts and "silently" relays the "matched" URL.

Breakdown of Regular Expressions and mod_rewite symbols:
Here's a small table describing some of the symbols and their meanings in the regular expressions we're using. This not an exhaustive list, just a few commonly used symbols.
| Symbol: | Explaination: |
| ^ | Start of a new line. ^example would match example on a new line, but not in the middle of a sentance. |
| $ | End of a line example$ would match example at the end of a line, but not in the middle of a sentance. |
| ? | Optional, e.g. "colou?r" would match color and colour, articles/example/? would cause the ending / to be optional. |
| ([0-9]+) | Would match only numbers. |
| ([a-z]+) | Matches lowercase characters. |
| ([A-Z]+) | Matches uppercase characters. |
| ([a-zA-Z]+) | Matches both upper and lowercase. |
| ([^/.]+) | Matches any character except / (in this case, a ^ means "not"). |
| [L] | Tells mod_rewrite to stop and not bother checking other rules if a match is found. |
| $1, $2... | The "matched" value, should you need to use it. ^articles/([0-9]+)/?$ articles.php?id=$1 |
You'll need to edit your .htaccess file for this, it is a file called ".htaccess" located in your public_html (or web root directory) . Create it if it's not there. The first line simply starts the mod_rewrite engine, it's only needed once at the top of your file.
RewriteEngine On
RewriteRule ^articles$ articles.php [L]
The above rule matches any url that "starts" with "articles" and also ends with "articles". It then "silently" redirects to articles.php if a match is found. It will not match "test-articles", or "articles_january" for example.
In the next example, we'll pass along to the server, a query sting. As you can see, this will match articles/1 or articles/2 and redirect it to articles.php?id=1. The second example, adds /? which means articles/1/ with a trailing slash would also work.
RewriteRule ^articles/([0-9]+)$ articles.php?id=$1 [L]
RewriteRule ^articles/([0-9]+)/?$ articles.php?id=$1 [L] # trailing slash optional
Getting a bit more tricky, you could add another argument to the above examples to add a "page" argument:
RewriteRule ^articles/([0-9]+)/page/([0-9]+)/?$ articles.php?id=$1&p=$2 [L]
You can of course, replace [0-9] with one of the symbols in the table above, to match letters, or other characters.
Tips and Tricks:
There are a few nice tips and tricks you can do using mod_rewrite. Perhaps, you want to force everyone to use your website without the www. You could add the following rule to do that: Note that ! effectively means "not", and [R] means, redirect. RewriteCond is a way that you can apply an "if this, do something" type rule.
RewriteCond %{HTTP_HOST} !^example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R,L]
Suppose you wanted to redirect a certain page to secure https:// you could use a rule such as:
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
Summary:
I hope that this article has provided a good introduction how mod_rewrite can work for you, and the very basics of regular expressions. If you have any other tricks or tips you wish to share, please feel free to comment!
Please make sure to give it a through testing because regular expression can mess things u if not used properly...Also check out my post below on search engine optimization...
http://www.andhapp.com/blog/post/Rewriting-URLs.../44





#1 Ray says:
another good option to use is [QSA], it will automatically append any query parameters.
eg. say you have a rewrite rule that directs page.com/browse/articles to browse.php?c=articles
if you wanted to put ordering onto the page to sort by date or some other critieria your url might end up as
page.com/browse/articles/?sort=date
adding [QSA] will change the url to:
browse.php?c=articles&sort=date