mod_rewrite :
URL Rewriting and Translation Engine
This module uses a rule-based rewriting engine (based on a regular-expression
parser) to rewrite requested URLs on the fly. It supports an unlimited number
of rules and an unlimited number of attached rule conditions for each rule
to provide a really flexible and powerful URL manipulation mechanism. The
URL manipulations can depend on various tests, for instance server variables,
environment variables, HTTP headers, time stamps and even external database
lookups in various formats can be used to achieve a really granular URL
matching.
Why should I rewrite the original URL
- Improves Seach Engine Indexing - In general, search engines
love clean and keyword rich URLs instead of URLs that full of parameters
and session variables. For example,
/articles/search-engine/20040307/
will definately prefered than
/index.php?type=article&category=search-engine&date=20040307
Such search engine friendly URLs can encourage deep crawl and eventually
improves your rankings. Imagine you have a forum with 1000 threads,
all of them are identified by viewtopic.php?t=34&sid=308e633eb1136a8ae8d781ed589a4082
Search engines will be given a different session id(sid) while crawling
all the threads(t), this will confused search engines as they might
be crawling the same content with different URLs because of the session
id, and eventually search engines might just thread viewtopic.php as
1 page instead of 1000 pages. This is just to illustrate the important
of clean and friendly URLs as most of the major boards have already
fixed this problem.
- Maintain Consistency of URLs - At some point, you might want
to redesign the site or the structure which may cause some changes in
the linking structure and yet you don't want all the bookmarked links
become dead, mod_rewrite can help to rewrite all the old URLs to your
new files.
mod_rewrite Example for Dummies
All the mod_rewrite rules need to be implemented inside .htaccess, which
need to be uploaded in ASCII mode. To start the module, these two entries
need to be included :
RewriteEngine
on #change to off to disable url rewriting
Options +FollowSymlinks
|
Now is time to implement your rules, I'll give two simple scenarios here
:
1. Assume we have recently renamed the page foo.html to bar.html and now
want to provide the old URL for backward compatibility, and we want this
to be transparent to users as well. We will need to add the following lines
to .htaccess to achieve this :
RewriteEngine on #change to off to disable url rewriting
Options +FollowSymlinks
RewriteBase / #to specify base URL
RewriteRule ^foo\.html$ bar.html
|
2. Assume we want to change the original URL to a keyword rich URL for search engine optimization
purposes, we will need to add the following lines to .htaccess to achieve this :
RewriteEngine on #change to off to disable url rewriting
Options +FollowSymlinks
RewriteBase / #to specify base URL
RewriteRule ^webmaster-forum(.*)$ index.php?c=8
|
After the rules are included, www.forumshowcase.com/webmaster-forum/
will be mapped to the actual URL www.forumshowcase.com/index.php?c=8
, while both of them still accessing the same content.
Regular Expression
A regular expression(regex), is a special text string for describing a search
pattern. You can implement a very powerful mod_rewrite rules by having a
complicated regex. Below are some important elements :
| .(dot) |
- text
(any character) |
| | |
- alternation
(i.e. /abc|def/) |
| * |
- quantifier
(any number is allowed) |
| ^ $
|
- line
anchors |
| s |
- operator
(string1 to be replaced by string2) |
| g |
- modifier
(search parses the whole text) |
Regular expressions are construed with the help of these elements and alphanumeric
characters.
This article is intended to give a brief review of mod_rewrite, both the
examples are the simplest form that you can implement using mod_rewrite,
for more complex and detail explaination, please refer to the suggested
resources at below.
Useful Resources
|