Professional Documents
Culture Documents
Introduction to mod_rewrite
Rich Bowen, Web Guy, Asbury College
rbowen@apache.org
http://people.apache.org/~rbowen/
1
Outline
Regex basics
RewriteRule
RewriteCond
RewriteMap
2
mod_rewrite
is not magic
Fear, more than
complexity, makes
mod_rewrite difficult
3
Although, it is complex
4
And let’s not forget
voodoo!
`` Despite the tons of
examples and docs,
mod_rewrite is voodoo.
Damned cool voodoo,
but still voodoo. ''
-- Brian Moore
5
Line noise
"Regular expressions
are just line noise.
I hate them!"
(Heard 20 times per
day on IRC)
6
Now that that’s out of the way
patterns
7
Vocabulary
8
.
9
+
10
*
11
?
12
^
^ is called an anchor
13
$
14
()
15
( ), continued
( ) allows you to capture a match so that you
can use it later.
16
[]
[abc] matches a or or b or c
17
NOT
18
So, what does this have to do with
Apache?
mod_rewrite lets you match URLs (or other
things) and transform the target of the URL
based on that match.
RewriteEngine On
# Burninate ColdFusion!
RewriteRule (.*)\.cfm$ $1.php [PT]
# And there was much rejoicing. Yaaaay.
19
RewriteEngine
“RewriteEngine On” enables
the mod_rewrite rewriting
engine
20
RewriteLog
RewriteLog /www/logs/rewrite_log
RewriteLogLevel 9
21
RewriteRule
RewriteRule pattern target [flags]
22
Example 1
23
URL beautification
A URL looks like:
http://example.com/cgi-bin/book.cgi?author=bowen&topic=apache
http://example.com/book/bowen/apache
24
Example 1, cont’d
RewriteRule ^/book/(.*)/(.*) \
/cgi-bin/book.cgi?topic=$1&author=$2 [PT]
26
By the way ...
Default is to treat the rewrite target as a
file path
28
RewriteRule flags
[Flag] appears at end of RewriteRule
29
Chain
[C] or [Chain]
30
Cookie
[CO=NAME:Value:Domain[:lifetime[:path]]
Sets a cookie
31
Env
[E=var:val]
32
Forbidden
[F] or [Forbidden] forces a 403 Forbidden
response
33
Handler
[H=application/x-httpd-php]
34
Last
[L] indicates that you’ve reached the end of
the current ruleset
35
Next
The [N] or [Next] flag is a good way to get
yourself into an infinite loop
36
NoCase
37
NoEscape
38
NoSubreq
39
Proxy
[P] rules are served through a proxy
subrequest
40
Passthrough
[PT] or [passthrough]
41
QSAppend
42
Redirect
43
Skip
44
Type
[T=text/html]
45
RewriteCond
Causes a rewrite to be conditional
46
RewriteCond
47
Looping
Looping occurs when the target of a rewrite
rule matches the pattern
RewriteCond %{REQUEST_URI} \
!^/example.html
RewriteRule ^/example /example.html [PT]
48
Conditional rewrites
Rewrites conditional on some arbitrary
thingy
RewriteEngine on
RewriteCond %{TIME_HOUR}%{TIME_MIN} >0700
RewriteCond %{TIME_HOUR}%{TIME_MIN} <1900
RewriteRule ^page\.html$ page.day.html
RewriteRule ^page\.html$ page.night.html
49
SSL Rewrites
Redirect requests to https:// if the request
was for http
50
RewriteMap
51
RewriteMap - file
File of one-to-one relationships
Alias http://httpd.apache.org/docs/mod_alias.html#alias
Redirect http://httpd.apache.org/docs/mod_alias.html#redirect
... etc
52
Poor-man’s load balancing
Random selection of server for “load
balancing”
servers.txt contains:
loadbalance mars|jupiter|saturn|neptune
54
RewriteMap - program
Call an external program to do the rewrite
RewriteMap dash2score \
prg:/usr/local/apache/conf/dash2score.pl
RewriteEngine On
RewriteRule (.*-.*) ${dash2score:$1} [PT]
55
dash2score.pl
#!/usr/bin/perl
$| = 1; # Turn off buffering
while (<STDIN>) {
s/-/_/g; # Replace - with _ globally
print $_;
}
57
.htaccess files
So ...
58
.htaccess files
In .htaccess files, or <Directory> scope,
everything is assumed to be relative to that
current scope
59
.htaccess files
RewriteLog is particularly useful when trying
to get .htaccess file RewriteRules working.
60
.htaccess files
The rewrite pattern is relative to the
current directory
61
Further resources
http://rewrite.drbacchus.com/
http://httpd.apache.org/docs-2.2/rewrite/
62
Questions?
63
Bonus slides - Recipes
64
RewriteEngine On
RewriteCond %{REQUEST_URI} !handler.php
RewriteRule (.*) /handler.php?$1 [PT,L,NE]
65
Virtual Hosts
66
RewriteEngine On
RewriteCond %{HTTP_HOST} (.*)\.example\.com [NC]
RewriteRule (.*) /home/%1/www$1
67
.phps source handler
RewriteRule (.*)\.phps \
$1.php [H=application/x-httpd-php-source]
Syntax-highlighted code
rendering of any .php
file
68