Ajax software
Free javascripts
↑
Main Page
The second line specifies the rewrite rule using the mod_rewrite
RewriteRule
command. You use this
to have mod_rewrite translate
my-super-product.html
to
product.php?product_id=123
. The line
that precedes the
RewriteRule
line is a comment; comments are marked using the pound (character)
at the beginning of the line, and are ignored by the parser:
# Translate my-super.product.html to /product.php?product_id=123
RewriteRule ^my-super-product\.html$ /product.php?product_id=123
You can find the official documentation for
RewriteRule
at
http://www.apacheref.com/
ref/mod_rewrite/RewriteRule.html
.
In its basic form,
RewriteRule
takes two parameters. The first parameter
describes
the original URL that
needs to be rewritten, and the second specifies what it should be rewritten to. The pattern that describes the
original URL is delimited by
^
and
$
, which assert that the string has nothing before or after the matching
text (explained further in the following sections), and its contents are written using
regular expressions
,
which you learn about next.
In case you were wondering why the
.html
extension has been written as
\.html
in the rewrite rule,
we will explain it now. In regular expressions — the programming language used to describe the original
URL that needs to be rewritten — the dot is a character that has a special significance. If you want that
dot to be read as a literal dot, you need to escape it using the backslash character. As you’ll learn, this
is a general rule with regular expressions: when special characters need to be read literally, they need
to be escaped with the backslash character (which is a special character in turn — so if you wanted to
use a backslash, it would be denoted as
\\
).
Introducing Regular Expressions
Many love regular expressions, while others hate them. Many think they’re very hard to work with, while
many (or maybe not so many) think they’re a piece of cake. Either way, they’re one of those topics you can’t
avoid when URL rewriting is involved. We’ll try to serve a gentle introduction to the subject, although
entire books have been written on the subject. You can even find a book dedicated to mod_rewrite, which
Using RewriteBase
The regular expressions and scripts in this book assume that your application runs in
the root folder of their domain. This is the typical scenario. If, however, you host your
application in a subfolder of your domain, such as
http://www.example.com/seophp
, you’d need to make a few changes to accommo-
date the new environment.
The most important change would be to use the
RewriteBase
directive of mod_rewrite
to specify the new location to act as a root of your rewriting rules. This directive is
explained at
http://www.apacheref.com/ref/mod_rewrite/RewriteBase.html
.
Also, the rewritten URL should lose its leading slash, because you’re not rewriting to
root any more. Basically, if you host your first example in a subfolder named
seophp
,
your
.htaccess
file for the previous exercise should look like this:
RewriteEngine On
RewriteBase /seophp
RewriteRule ^my-super-product\.html$ product.php?product_id=123
54
Chapter 3: Provocative SE-Friendly URLs
c03.qxd:c03 10:39 54
Ajax software
Free javascripts
→ R7