Upload
john-ramesh
View
218
Download
0
Embed Size (px)
Citation preview
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 1/96
/
mod_rewrite
Introduction to mod_rewriteRich Bowen, Web Engineer, OmniTI
http://people.apache.org/~rbowen/
http://httpd.apache.org/docs/trunk/rewrite
Thursday, November 5, 2009
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 2/96
Outline
• Regex basics
• RewriteRule
• RewriteCond
• RewriteMap
• The evils of .htaccess files• Recipes
Thursday, November 5, 2009
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 3/96
mod_rewrite is not magic
• Fear, more than
complexity,makesmod_rewritedifficult
Thursday, November 5, 2009
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 4/96
Although, it is complex
``The great thing aboutmod_rewrite
is it gives you all the configurabilityand flexibility of Sendmail. The downside to
mod_rewrite is that it gives you allthe configurability and flexibility of
Sendmail.''
-- Brian Behlendorf
Thursday, November 5, 2009
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 5/96
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
Thursday, November 5, 2009
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 6/96
Line noise
R e g u l a
r e x p r e s
s i o n s a r e
j u s t l i n e
n o i s e.
I h a t e t h
e m !
And if you see some of theexamples that show up onIRC, you’d think that too ...
Thursday, November 5, 2009
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 7/96
Now that that’s out of the way
• Regular expressions are not magic
• They are an algebraic expression of text
patterns
• Once you get over the mysticism, it can
still be hard, but it's no longer mysterious
Thursday, November 5, 2009
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 8/96
Vocabulary
• We’re going to start with a very small
vocabulary, and work up from there
• Most of the time, this vocabulary is allthat you’ll need
Thursday, November 5, 2009
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 9/96
.
•. matches any character
• “a.b” matches acb, axb, a@b, and so on
• It also matches Decalb and Marbelized
Thursday, November 5, 2009
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 10/96
Repetition
• + means that something needs toappear one or more times: ‘a+’matches ‘a’, ‘aa’, ‘aaa’, and ‘Mondrian’
• * matches zero or more. ‘a*’ matches‘a’, ‘aa’, and the empty string (‘’)
• ? means that the match is optional.‘colou?r’ matches ‘color’ and ‘colour’
Thursday, November 5, 2009
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 11/96
Anchors
• ^ means “starts with”. ‘^a’ matches‘alpha’ and ‘arnold’
• $ means “ends with”. ‘a$’ matches‘alpha’ and ‘stella’
• ‘^$’ matches an empty string
• ‘^’ matches every string (every stringhas a start)
Thursday, November 5, 2009
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 12/96
( ) - Grouping
• ( ) allows you to group several
characters into one thingy, and apply other modifiers to it.
• “(ab)+” matches ababababababab
Thursday, November 5, 2009
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 13/96
( ) - Backreferences
• ( ) allows you to capture a match sothat you can use it later (called a
backreference)
• It might be called $1 or %1 dependingon the context
• The second match is called $2 (or%2) and so on
Thursday, November 5, 2009
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 14/96
[ ]
• [ ] defines a “character class”
•[abc] matches a or or b or c
• “c[uoa]t” matches cut, cot, or cat
• It also matches cote
• It does not match coat
Thursday, November 5, 2009
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 15/96
NOT
• In mod_rewrite regularexpressions, ! negates any match
• In a character class, ^ negates thecharacter class
• [^ab] matches any character except
for a or b.
Thursday, November 5, 2009
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 16/96
So, what does this have to do with Apache?
• mod_rewrite lets you match URLs (or otherthings) and transform the target of the URL based on that match.
RewriteEngine OnRewriteRule (.*)\.cfm$ $1.php [PT]
Thursday, November 5, 2009
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 17/96
RewriteEngine
• “RewriteEngine On”enables the mod_rewriterewriting engine
• No rewrite rules will beperformed unless this isenabled in the activescope
Thursday, November 5, 2009
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 18/96
RewriteLog
RewriteLog /www/logs/rewrite_logRewriteLogLevel 9
You should turn on the RewriteLog before you doany troubleshooting.
Thursday, November 5, 2009
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 19/96
RewriteRule pattern target [flags]
• The pattern part is the regular expressionthat you want to look for in the URL
• If they try to go HERE send them HEREinstead.
• The behavior can be further modified by the
use of one or more flags
RewriteRule
Thursday, November 5, 2009
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 20/96
URL beautification
• A URL looks like:
.com/cgi-bin/book.cgi?author=bowen&topic=apache
We would prefer that it looked like
It’s easier to type, and easier to
remember
Thursday, November 5, 2009
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 21/96
Example 1, cont’d
• User does not notice that the transformation has been made
• Used $1 and $2 to capture what was requested
• Slight oversimplification. Should probably use([^/]+)
instead.
RewriteRule ^/book/(.*)/(.*) \/cgi-bin/book.cgi?topic=$1&author=$2 [PT]
Thursday, November 5, 2009
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 22/96
Example 1, cont’d
• Should probably use([^/]+) instead of (.*)
• (.*) is frequently used when something else would be faster, or at leastmore correct.
RewriteRule ^/book/([^/]+)/([^/]+) \/cgi-bin/book.cgi?topic=$1&author=$2 [PT]
Thursday, November 5, 2009
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 23/96
Flags
• Flags can modify the behavior of a RewriteRule• I used a flag in the example, and didn’t tell you
what it meant
Thursday, November 5, 2009
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 24/96
Default
• Default is to treat the rewrite target asa file path
•If the target starts in http:// or
https:// then it is treated as a URL,and a [R] is assumed (Redirect)
• In a .htaccess file, or in <Directory>
scope, the file path is assumed to berelative to that scope
Thursday, November 5, 2009
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 25/96
RewriteRule flags
• [Flag] appears at end of RewriteRule
• More than one flag separated by commas- eg [R,L,NE] (no spaces)
• There’s *lots* of flags
Thursday, November 5, 2009
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 26/96
Escape Backreferences
• [B] (There’s no long version)
• Causes backreferences to be escaped to preserve special characters
• The former will collapse escaped characters (convert %2b to + forexample) while the latter will preserve them (stay as %2b)
RewriteRule ^(.*)$ index.php?show=$1
RewriteRule ^(.*)$ index.php?show=$1 [B]
Thursday, November 5, 2009
k
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 27/96
Cookie
• [CO=NAME:Value:Domain[:lifetime[:path]]
• Long form [cookie=...]
• Sets a cookie
RewriteRule ^/index.html - [CO=frontdoor:yes:.example.com]
In this case, the default values for path(”/”) and lifetime (”session”) areassumed.
Thursday, November 5, 2009
i d h f
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 28/96
Discard Path Info
• [DPI] or [discardpathinfo]
• Discards additional PathInfo bits thatmay have accumulated via rewriting
Thursday, November 5, 2009
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 29/96
Env
• [E=var:val]
• Long form [env=...]
• Sets environment variable
•Note that most of the time, SetEnvIf works just fine
RewriteRule \.(gif|jpg|png)$ - [env=image:1]
CustomLog /var/log/access_log \combined env=!image
Thursday, November 5, 2009
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 30/96
Forbidden
• [F] or [Forbidden] forces a 403 Forbidden response
• Consider mod_security instead for pattern-based URL blocking
RewriteEngine OnRewriteRule (cmd|root)\.exe - [F]
You could use this in conjunction with [E] to
avoid logging that stuff RewriteRule (cmd|root)\.exe - [F,E=dontlog:1]
CustomLog /var/log/apache/access_log combined \
env=!dontlog
Thursday, November 5, 2009
H dl
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 31/96
Handler
• [H=application/x-httpd-php]
• Forces the use of a particular
handler to handle the resultingURL
RewriteEngine On
RewriteRule !\. - \[H=application/x-httpd-php]
Thursday, November 5, 2009
L t
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 32/96
Last
• [L] indicates that you’ve reached theend of the current ruleset
• Any rules following this will beconsidered as a completely new ruleset
• It’s a good idea to use it, even when it would otherwise be default behavior. Ithelps make rulesets more readable.
Thursday, November 5, 2009
[L] i ht fil
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 33/96
[L] in .htaccess files
• [L] means to hand the resulting URI back to the URI-mapping process.
• Part of this process may result in
the .htaccess file being invoked again.
• Thus, in .htaccess files, [L] can initiateloops.
• Use RewriteCond to avoid loops (seeexamples later)
Thursday, November 5, 2009
P
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 34/96
Proxy
• [P] rules are served through a proxy subrequest
• mod_proxy must be installed for this
flag to work RewriteEngine OnRewriteRule (.*)\.(jpg|gif|png) \
$1.$2 [P]
Thursday, November 5, 2009
P th h
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 35/96
Passthrough
• [PT] or [passthrough]
• Hands it back to the URL mappingphase
• Treat this as though this was theoriginal request
Thursday, November 5, 2009
QSA d
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 36/96
QSAppend
• [QSA] or [qsappend] appends tothe query string, rather thanreplacing it.
Thursday, November 5, 2009
R di t
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 37/96
Redirect
• [R] or [redirect] forces a 302Redirect
• Note that in this case, the user willsee the new URL in their browser
• This is the default behavior when
the target starts with http:// orhttps://
Thursday, November 5, 2009
Redirect
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 38/96
Redirect
•Can designate a different redirectstatus code with [R=305]
• Can even do [R=404] if you want.
Thursday, November 5, 2009
Skip
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 39/96
Skip
•[S=n] or [skip=n] skips the next n
RewriteRules
• Can be used for negation of a block of rules - as a sort of inverse RewriteCond
# Don’t run these rules if its an imageRewriteRule ^/images - [S=4]
Thursday, November 5, 2009
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 40/96
RewriteCond and [S]
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 41/96
RewriteCond and [S]
• DOESN’T WORK
# Block One
RewriteCond %{HTTP_HOST} example.comRewriteRule \.gif$ /something1.htmlRewriteRule \.jpg$ /something2.htmlRewriteRule \.ico$ /something3.html
RewriteRule \.png$ /something4.html
# Block TwoRewriteRule ^ - http://something.else.com/ [R]
Thursday, November 5, 2009
RewriteCond and [S]
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 42/96
RewriteCond and [S]
• Solution: Use an [S] flag as a GoTo statement
# Block OneRewriteCond !%{HTTP_HOST} example.comRewriteRule ^ - [S=4]RewriteRule \.gif$ /something1.htmlRewriteRule \.jpg$ /something2.htmlRewriteRule \.ico$ /something3.html
RewriteRule \.png$ /something4.html
# Block TwoRewriteRule ^ - http://something.else.com/ [R]
Thursday, November 5, 2009
Type
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 43/96
Type
• [T=text/html]
• Forces the Mime type on the resulting URL
• Good to ensure that file-path redirects are handled correctly
RewriteRule ^(.+\.php)s$ $1 [T=application/x-httpd-php-source]
Thursday, November 5, 2009
Others
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 44/96
Others
• [NC] or [nocase] makes theRewriteRule case insensitive
• [NE] or [NoEscape] - Don’t URL-escape the results
• [NS] - Don’t run on subrequests
Thursday, November 5, 2009
Infrequently used
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 45/96
Infrequently used
• [C] or [Chain] - Rules are consideredas a whole. If one fails, the entire chainis abandoned
• [N] or [Next] - Start over at the top.Useful for rules that run in a while loop
RewriteRule (.+)-(.+) $1_$2 [N]
Thursday, November 5, 2009
RewriteCond
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 46/96
RewriteCond
• Causes a rewrite to be conditional• Can check the value of any variable and
make the rewrite conditional on that.
RewriteCond TestString Pattern [Flags]
Thursday, November 5, 2009
RewriteCond
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 47/96
RewriteCond
•Test string can be Env vars,headers, or a literal expression
• Backreferences become %1, %2, etc
Thursday, November 5, 2009
Example - Looping
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 48/96
Example - Looping
•Looping occurs when the target of arewrite rule matches the pattern
• This results in an infinite loop of rewrites
RewriteRule ^/example /example.html [PT]
Thursday, November 5, 2009
Example - Looping
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 49/96
Example Looping
•Looping occurs when the target of arewrite rule matches the pattern
• This results in an infinite loop of rewrites
RewriteRule ^/example /example.html [PT]
Thursday, November 5, 2009
Example - Looping
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 50/96
Example Looping
• Solution: use RewriteCond to excludethat condition.
RewriteCond %{REQUEST_URI} \ !^/example.html
RewriteRule ^/example /example.html [PT]
Thursday, November 5, 2009
Conditional rewrites
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 51/96
Conditional rewrites
• Rewrites conditional on some arbitrary thingy
• Only first Rule is dependent
RewriteEngine on
RewriteCond %{TIME_HOUR}%{TIME_MIN} >0700
RewriteCond %{TIME_HOUR}%{TIME_MIN} <1900RewriteRule ^page\.html$ page.day.html
RewriteRule ^page\.html$ page.night.html
Thursday, November 5, 2009
Conditional rewrites
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 52/96
Conditional rewrites
• At 08:30 ...
RewriteEngine on
RewriteCond %{TIME_HOUR}%{TIME_MIN} >0700
RewriteCond %{TIME_HOUR}%{TIME_MIN} <1900RewriteRule ^page\.html$ page.day.html
RewriteRule ^page\.html$ page.night.html
Thursday, November 5, 2009
Conditional rewrites
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 53/96
Conditional rewrites
• Could also use an [L] flag here
RewriteEngine on
RewriteCond %{TIME_HOUR}%{TIME_MIN} >0700
RewriteCond %{TIME_HOUR}%{TIME_MIN} <1900
RewriteRule ^page\.html$ page.day.html [L]
RewriteRule ^page\.html$ page.night.html
Thursday, November 5, 2009
Conditional rewrites
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 54/96
Conditional rewrites
• At 21:49 ...
RewriteEngine on
RewriteCond %{TIME_HOUR}%{TIME_MIN} >0700
RewriteCond %{TIME_HOUR}%{TIME_MIN} <1900RewriteRule ^page\.html$ page.day.html
RewriteRule ^page\.html$ page.night.html
Thursday, November 5, 2009
SSL Rewrites
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 55/96
SSL Rewrites
• Redirect requests to https:// if the request was for http
RewriteCond %{HTTPS} !onRewriteRule (.*) https://%{HTTP_HOST}/$1 [R]
Thursday, November 5, 2009
Force a hostname
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 56/96
Force a hostname
RewriteCond %{HTTP_HOST} !www.example.com RewriteRule (.*) http://www.example.com/$1 [R=301]
Thursday, November 5, 2009
Force a hostname
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 57/96
Force a hostname
RewriteCond %{HTTP_HOST} !www.example.com RewriteRule (.*) http://www.example.com/$1 [R=301]
Thursday, November 5, 2009
RewriteMap
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 58/96
p
• Call an external program, or map file, toperform the rewrite
• Useful for very complex rewrites, orperhaps ones that rely on somethingoutside of Apache
Thursday, November 5, 2009
RewriteMap - file
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 59/96
p
• File of one-to-one relationships
RewriteMap dogmap txt:/www/conf/dogmap.txtRewriteRule ^/dog/(.*) ${dogmap:$1} [NE,PT]
Where dogmap.txt contains:
doberman /dogs.php?breed=278poodle /dogs.php?breed=78collie /dogs.php?breed=98terrier /dogs.php?breed=148mutt /dogs.php?breed=2alsatian /dogs.php?breed=113
Requests for http://example.com/dog/something now getredirected to the page for that breed. [NE] ensures thatthe ? doesn’t get escaped.
Thursday, November 5, 2009
dbm
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 60/96
•Convert a one-to-one text mapping to a dbmfile
• httxt2dbm utility does this conversion
RewriteMap dogmap \dbm:/www/conf/dogs.map
Thursday, November 5, 2009
RewriteMap - program
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 61/96
p p g
• Call an external program to do the
rewrite• Perl is a common choice here, due to its
skill at handlin text.
RewriteMap dash2score \prg:/usr/local/apache/conf/dash2score.pl
RewriteEngine OnRewriteRule (.*-.*) ${dash2score:$1} [PT]
Thursday, November 5, 2009
dash2score.pl
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 62/96
p
#!/usr/bin/perl
$| = 1; # Turn off bufferingwhile (<STDIN>) {s/-/_/g; # Replace - with _ globallyprint $_;
}
* Turn off buffering
* Script runs for lifetime of Apache process* Blocking - use RewriteLock
Thursday, November 5, 2009
SQL (in 2.3-HEAD)
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 63/96
Q ( 3 )
• Have a SQL statement in the RewriteMap directive which returns themapping
• ‘fastdbd’ caches, ‘dbd’ doesn’t
RewriteMap myquery "fastdbd:SELECT destinationFROM rewrite WHERE source = %s"
Thursday, November 5, 2009
.htaccess files
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 64/96
• .htaccess files introduce many additional complexities
• However, a lot of people have nochoice
•So ...
Thursday, November 5, 2009
.htaccess files
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 65/96
• In .htaccess files, or <Directory> scope,everything is assumed to be relative to thatcurrent scope
• So, that scope is removed from theRewriteRule
• ^/index.html in httpd.conf becomes^index.html in a .htaccess file or
<Directory> scope
Thursday, November 5, 2009
.htaccess files
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 66/96
# In httpd.conf RewriteRule ^/images/(.+)\.jpg /images/$1.png
# In .htaccess in root dir
RewriteBase /RewriteRule ^images/(.+)\.jpg images/$1.png
# In .htaccess in images/RewriteBase /images/
RewriteRule ^(.+)\.jpg $1.png
Thursday, November 5, 2009
.htaccess files
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 67/96
• RewriteLog is particularly useful whentrying to get .htaccess file RewriteRules
working.
• However, you can’t turn on RewriteLog ina .htaccess file, and presumably you’reusing .htaccess files because you don’t haveaccess to the main server config.
• It’s a good idea to set up a test server andtest there with RewriteLog enabled
Thursday, November 5, 2009
Redirect to one thing
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 68/96
RewriteEngine OnRewriteCond %{REQUEST_FILENAME} !-dRewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_URI} !handler.php
RewriteRule (.*) /handler.php?$1 [PT,L,NE,QSA]
All requests are sent to handler.php
The request is passed as a QUERY_STRINGargument to handler.php so that it knows what was
requested.
Redirect to one thing
Thursday, November 5, 2009
FallbackResource
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 69/96
RewriteEngine OnRewriteCond %{REQUEST_FILENAME} !-dRewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_URI} !handler.php
RewriteRule (.*) /handler.php?$1 [PT,L,NE,QSA]
In 2.4, this is replaced with a single directive,FallbackResource:
FallbackResource
FallbackResource /handler.php
Thursday, November 5, 2009
Query String
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 70/96
• RewriteRule doesn’t have access to the Query String
# Rewrite based on query stringRewriteCond %{QUERY_STRING} \
\bfoo=(.*?)\b
RewriteRule /something /somewhere/%1 [QSA]
Thursday, November 5, 2009
Query String
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 71/96
• “word” boundaries
# Rewrite based on query stringRewriteCond %{QUERY_STRING} \ \bfoo=(.*?)\b
RewriteRule /something /somewhere/%1 [QSA]
Thursday, November 5, 2009
Virtual Hosts
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 72/96
• Rewrite a request to a directory based onthe requested hostname.
Thursday, November 5, 2009
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 73/96
• The hostname ends up in %1
• The requested path is in $1 - includesleading slash
• Will probably have to do special thingsfor handlers (like .php files)
RewriteEngine On
RewriteCond %{HTTP_HOST} (.*)\.example\.com [NC]RewriteRule (.*) /home/%1/www$1
Thursday, November 5, 2009
Dav upload php
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 74/96
•Upload php, then execute it. Bad.
RewiteEngine OnRewriteCond %{REQUEST_METHOD} =PUT [OR]RewriteCond %{REQUEST_METHOD} =MOVERewriteRule ^/dav/(.*)\.php /dav/$1.nophp
Thursday, November 5, 2009
PHP when no file ext
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 75/96
• Force files with no file extension to behandled by php
Thursday, November 5, 2009
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 76/96
• Allows you to have URLs without the annoying“.php” on the end.
RewriteEngine On
RewriteRule !\. - [H=application/x-httpd-php]
Thursday, November 5, 2009
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 77/96
• Doesn’t contain a dot
RewriteEngine On
RewriteRule !\. - [H=application/x-httpd-php]
Thursday, November 5, 2009
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 78/96
• Don’t rewrite it
RewriteEngine On
RewriteRule !\. - [H=application/x-httpd-php]
Thursday, November 5, 2009
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 79/96
• Force it to use the php handler
RewriteEngine On
RewriteRule !\. - [H=application/x-httpd-php]
Thursday, November 5, 2009
Use PATH_INFO
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 80/96
• Now you can have URLs like
• Use $_SERVER[PATH_INFO] to grabthe additional bits of the request
http://example.com/handler/arg1/arg2
Thursday, November 5, 2009
mod_negotiation
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 81/96
•Might be able to do the same thing with
mod_negotiation
• Options +MultiViews
Thursday, November 5, 2009
<If>
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 82/96
•Just added
• Makes much of mod_rewriteunnecessary.
• http://httpd.apache.org/docs/trunk/mod/core.html#if
<If "$req{Host} = ‘myhost.com’">
Thursday, November 5, 2009
<If>
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 83/96
• Variable can be in $req, $resp, or $env
• Any Request, Response, orEnvironment variable
<If "$env{foo} = ‘bar’">...
</If>
Thursday, November 5, 2009
Related modules
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 84/96
• mod_substitute
• mod_ext_filter• mod_proxy_html
• mod_line_edit
Thursday, November 5, 2009
How do I do that ...
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 85/96
• Questions like “How do I do XYZ withmod_rewrite” often have the same answer
• YOU DON’T
• These modules are sometimes the rightanswer
Thursday, November 5, 2009
mod_substitute
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 86/96
• New in 2.2.8
• In-stream regex
• Replace a string, or a pattern, in theoutput
•Chain with other filters
Thursday, November 5, 2009
mod_substitute
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 87/96
• One directive: Substitute
<Location />
AddOutputFilterByType SUBSTITUTE text/html
Substitute s/ariel/verdana/ni </Location>
Thursday, November 5, 2009
mod_substitute
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 88/96
• n = treat as a fixed string
• Default - treat as regex
<Location />
AddOutputFilterByType SUBSTITUTE text/html
Substitute s/ariel/verdana/ni </Location>
Thursday, November 5, 2009
mod_substitute
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 89/96
• i - Case insensitive match
• Default - Case sensitive
• n - string replacement, rather than regex
<Location />
AddOutputFilterByType SUBSTITUTE text/html
Substitute s/ariel/verdana/ni </Location>
Thursday, November 5, 2009
mod_substitute
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 90/96
• Replace ariel with verdana everywhere
• Filter content as it passes through.Perhaps on a proxy server.
<Location />
AddOutputFilterByType SUBSTITUTE text/html
Substitute s/ariel/verdana/ni </Location>
Thursday, November 5, 2009
More usefully ...
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 91/96
• Replace hard-coded hostnames in HTML
proxied from a back-end
• s/intranet.local/www.corpsite.com/i
Thursday, November 5, 2009
mod_ext_filter
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 92/96
• Calls an external command to filter the
stream
• Hugely inefficient
Thursday, November 5, 2009
mod_proxy_html
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 93/96
• Rewrites HTML at the proxy
• Swap hostnames for absolute URLs
• Third-party module
Thursday, November 5, 2009
mod_line_edit
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 94/96
• Very similar to mod_substitute• Third-party module
Thursday, November 5, 2009
Further resources
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 95/96
• http://rewrite.drbacchus.com/
• http://people.apache.org/~rbowen
• “Definitive Guide to mod_rewrite” by Rich Bowen, from APress
• http://httpd.apache.org/docs/2.2/
rewrite/
Thursday, November 5, 2009
Questions?
8/3/2019 48390461-mod-rewrite
http://slidepdf.com/reader/full/48390461-mod-rewrite 96/96
http://people.apache.org/~rbowen/