This document supplements the
We wish to dynamically generate content, but store it statically once it is generated. This rule will check for the existence of the static file, and if it's not there, generate it. The static files can be removed periodically, if desired (say, via cron) and will be regenerated on demand.
The -U
operator determines whether the test string
(in this case, REQUEST_URI
) is a valid URL. It does
this via a subrequest. In the event that this subrequest fails -
that is, the requested resource doesn't exist - this rule invokes
the CGI program /regenerate_page.cgi
, which generates
the requested resource and saves it into the document directory, so
that the next time it is requested, a static copy can be served.
In this way, documents that are infrequently updated can be served in static form. if documents need to be refreshed, they can be deleted from the document directory, and they will then be regenerated the next time they are requested.
We wish to randomly distribute load across several servers using mod_rewrite.
We'll use
serverlist.txt
will contain a list of the servers:
If you want one particular server to get more of the load than the others, add it more times to the list.
Apache comes with a load-balancing module -
Some sites with thousands of users use a
structured homedir layout, i.e. each homedir is in a
subdirectory which begins (for instance) with the first
character of the username. So, /~larry/anypath
is /home/l/larry/public_html/anypath
while /~waldo/anypath
is
/home/w/waldo/public_html/anypath
.
We use the following ruleset to expand the tilde URLs into the above layout.
By default, redirecting to an HTML anchor doesn't work,
because mod_rewrite escapes the #
character,
turning it into %23
. This, in turn, breaks the
redirection.
Use the [NE]
flag on the
RewriteRule
. NE stands for No Escape.
We wish to use mod_rewrite to serve different content based on the time of day.
There are a lot of variables named TIME_xxx
for rewrite conditions. In conjunction with the special
lexicographic comparison patterns <STRING
,
>STRING
and =STRING
we can
do time-dependent redirects:
This provides the content of foo.day.html
under the URL foo.html
from
07:01-18:59
and at the remaining time the
contents of foo.night.html
.
At times, we want to maintain some kind of status when we perform a rewrite. For example, you want to make a note that you've done that rewrite, so that you can check later to see if a request came via that rewrite. One way to do this is by setting an environment variable.
Use the [E] flag to set an environment variable.
Later in your ruleset you might check for this environment variable using a RewriteCond:
Note that environment variables do not survive an external redirect. You might consider using the [CO] flag to set a cookie. For per-directory and htaccess rewrites, where the final substitution is processed as an internal redirect, environment variables from the previous round of rewriting are prefixed with "REDIRECT_".