8. Expressions
Function by vestman, on Flickr
Mathematical statement which is either true
or false
Can consider any variable that interests
you
9. <If "%{HTTP_HOST} != 'www.example.com'">
Redirect permanent / http://www.example.com
</If>
<If> can be used in any
context (server, vhost,
directory, .htaccess)
10. Goodbye mod_rewrite
Doesn't replace mod_rewrite in *all*
contexts
But it does for many of the things you're
using it for
And it's much more readable, usually
13. Becomes:
Note: Shorter is not
always better. I think
this is *clearer*
<If "%{HTTP_REFERER} !~ www.example.com">
<If "%{REQUEST_URI} =~ .(gif|jpg|png)$">
Require all denied
</If>
</If>
14. Also
<If "-R '10.1.0.0/16'">
# ...
</If>
<ElseIf "-R '10.0.0.0/8'">
# ...
</ElseIf>
<Else>
# ...
</Else>
-R is like
"%{REMOTE_ADDR} -ipmatch ..."
15. More later
We could give examples of this all day, but
let's move on to something else ...
By Roby Ferrari, on Flickr
17. You've all seen this:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
18. You've all seen this:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
This bit says "If mod_rewrite
RewriteBase /
is enabled", and is
RewriteRule ^index.php$ - [L]
completely unnecessary.
RewriteCond %{REQUEST_FILENAME} !-f
That's probably a rant for
RewriteCond %{REQUEST_FILENAME} !-d
another time.
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
19. You've all seen this:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond Annoying bit to keep
%{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
mod_rewrite happy
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
20. You've all seen this:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
If it's already
RewriteRule . /index.php [L]
index.php, don't bother
</IfModule>
# END WordPress
21. You've all seen this:
# BEGIN WordPress
If it's not a file, and it's
<IfModule mod_rewrite.c>
not a directory (ie, a
RewriteEngine On
valid resource)
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
22. You've all seen this:
# BEGIN WordPress
Send everything else to
<IfModule mod_rewrite.c>
index.php as a "front
RewriteEngine On
controller"
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
24. How it works
FallbackResource /index.php
Triggers just before the error
handlers, once other options
have been checked. This ensures
that existing resources (css, js,
images, etc) continue to work as
desired.
25. How it works
FallbackResource /index.php
Note that that's a URL path, not
just a file name. Otherwise it
gets grumpy.
27. No need to rewrite
RewriteRule ^/(.*) /index.php?$1
# or something like that ...
Instead ...
FallbackResource /index.php
$args = explode('/',
$_SERVER['REQUEST_URI'] );
Remember that the first character of a
URL path is always "/"
28. Robert Fornal, on Flickr
*Match
Directives: AliasMatch, RedirectMatch,
ProxyPassMatch
Containers: DirectoryMatch, LocationMatch,
FilesMatch
29. AliasMAtch
# Keep the images separate
AliasMatch ^/image/(.*).jpg$
/files/jpg.images/$1.jpg
AliasMatch ^/image/(.*).gif$
/files/gif.images/$1.gif
AliasMatch ^/image/(.*).png$
/files/png.images/$1.gif
32. Everything but ...
RedirectMatch ^/(?!images/)(.*)
http://dynamic.myhost.com/$1
This is called a zero-width
assertion. Zero-width
because it doesn't capture a
backreference
33. Everything but ...
RedirectMatch ^/(?!images/)(.*)
http://dynamic.myhost.com/$1
Matches anything that
DOESN'T start with 'images/'
34. Everything but ...
RedirectMatch ^/(?!images/)(.*)
http://dynamic.myhost.com/$1
Result: Everything that's not
an image goes over *there*
35. FilesMatch
Recommended way to configure .php files
with mod_php
<FilesMatch .php$>
SetHandler application/x-httpd-php
</FilesMatch>
36. FilesMatch
Recommended way to configure .php files
with mod_php
<FilesMatch .php$>
SetHandler application/x-httpd-php
</FilesMatch>
Avoids .php.txt files being
processed, for example
38. ProxyPassMatch
ProxyPassMatch ^/(.*.gif)$
http://backend.example.com:8000/$1
Security note: The target URL
shouldn't be modifiable by the URL. That is,
there must not be any way that a cleverly
crafted URL could result in the target URL
being something nefarious.
41. Consider:
ProxyPassMatch ^/(.*)$
http://backend.example.com$1
Gotcha
What if my URL was
http://backend.example.com.myserver.com/
http://yourserver.com/.myserver.com/index.php
42. Consider:
ProxyPassMatch ^/(.*)$
http://backend.example.com$1
Fortunately, this actually
shouldn't work, since
ProxyPassMatch will return a 500
if the target URL isn't valid
before the substitution. (ie, no
trailing slash)
43. Oh, all right
We'll do some mod_rewrite stuff, because
you'd go home sad if I didn't.
44. Did you know ...
You can now use expr in RewriteCond, which
is practically magic
47. -strmatch is a glob,
not a regex
RewriteCond expr
"! %{HTTP_REFERER} -strmatch '*://%{HTTP_HOST}/*'"
RewriteRule ^/images - [F]
48. RewriteCond expr
"! %{HTTP_REFERER} -strmatch '*://%{HTTP_HOST}/*'"
RewriteRule ^/images - [F]
If the referer doesn't look
like the source ...
49. RewriteCond expr
"! %{HTTP_REFERER} -strmatch '*://%{HTTP_HOST}/*'"
RewriteRule ^/images - [F]
Yes, this is yet another way
to prevent "hot linking" of
images.
51. Database rewrites
RewriteMap supports database queries in 2.4
RewriteMap myquery
"fastdbd:SELECT ID
FROM breeds WHERE name = %s"
RewriteRule ^/dogs/(.*)
/breeds.php?id=${myquery:$1|0} [PT]
52. Map Name
RewriteMap myquery
"fastdbd:SELECT ID
FROM breeds WHERE name = %s"
RewriteRule ^/dogs/(.*)
/breeds.php?id=${myquery:$1|0} [PT]
53. Argument
RewriteMap myquery
"fastdbd:SELECT ID
FROM breeds WHERE name = %s"
RewriteRule ^/dogs/(.*)
/breeds.php?id=${myquery:$1|0} [PT]
54. Default
RewriteMap myquery
"fastdbd:SELECT ID
FROM breeds WHERE name = %s"
RewriteRule ^/dogs/(.*)
/breeds.php?id=${myquery:$1|0} [PT]
55. Query is prepared and executed,
so sql injection is mitigated, but
you still need to be careful.
RewriteMap myquery
"fastdbd:SELECT ID
FROM breeds WHERE name = %s"
RewriteRule ^/dogs/(.*)
/breeds.php?id=${myquery:$1|0} [PT]
56. Use "fastdbd" to enable query
caching, "dbd" to do without
caching
RewriteMap myquery
"fastdbd:SELECT ID
FROM breeds WHERE name = %s"
RewriteRule ^/dogs/(.*)
/breeds.php?id=${myquery:$1|0} [PT]
57. Default value if there's no
returned value. If multiple
values, one is selected randomly
RewriteMap myquery
"fastdbd:SELECT ID
FROM breeds WHERE name = %s"
RewriteRule ^/dogs/(.*)
/breeds.php?id=${myquery:$1|0} [PT]
58. mod_proxy_html
New module in 2.4
Was available as third-party module in
earlier versions
Simplifies proxying to a back-end app
59. mod_proxy_html
Proxying to internal server which is
closed-source, or that you don't have time
to monkey with
Generated HTML has fully-qualified URLs:
69. Heartbeats
Mark McLaughlin, on Flickr
This is new
mod_heartmonitor and mod_heartbeat
verify that a server is alive, and now you
balance by what server is more idle