HTML and CSS Reference
In-Depth Information
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>302 Found</title>
</head><body>
<h1>Found</h1>
<p>The document has moved
<a href="http://www.cafeaulait.org/books/jdr/examples">here</a>
</p>
<hr>
<address>Apache/2 Server at www.cafeaulait.org Port 80</address>
</body></html>
Most users never see this. Instead, their browser silently and immediately redirects them to
www.cafeaulait.org/books/jdr/examples/ . I was able to move the page but keep the old links working.
Consult your server documentation to determine exactly how this is accomplished with different servers. In the
most popular server, Apache, this is achieved with mod_rewrite rules placed in the httpd.conf or .htaccess files.
For example, the preceding redirect is accomplished with this rule:
RewriteEngine On
RewriteOptions MaxRedirects=10 inherit
RewriteBase /
RewriteRule ^examples(.*) books/jdr/examples$1 [R]
Regular expressions indicate exactly what is rewritten and how. In this case, after turning on the engine and
setting a couple of options to prevent infinite redirect loops, the base for the rewrites is set to / , the root of the
URL hierarchy. This is where all further matches begin.
The actual rule looks for all URLs whose path component begins with /examples , followed by any number of
characters. The ^ character anchors this expression to the rewrite base of / set in the previous line. The .*
matches everything that comes after /examples , and the parentheses around .* enable us to refer back to
those matched characters in the next part of the expression as $1 .
The third string on the last line, books/jdr/examples$1 , is the replacement rule. It replaces the matched string
from the first line with books/jdr/examples/ and the piece that was matched by (.*) in the search string.
Finally, [R] means that the client should be told about the redirect by sending a 302 response. If we left it out,
the new data would still be sent to the client, but it would appear as though it had come from the old URL.
That's usually not what you want, because keeping the same page at several URLs reduces your search engine
impact. (However, such silent redirects are very useful if you're trying to put a sensible URL structure on top of
a messy internal database-backed system. WordPress uses this technique extensively, for example.) If you'd
rather send a 301 Moved Permanently response, change [R] to [R=301] .
This one rule creates many redirects. For instance:
/examples/chapters/01 is redirected to /books/jdr/examples/chapters/01.
/examples is redirected to /books/jdr/examples.
/examples/chapters/01/HelloWorld.java is redirected to
/books/jdr/examples/chapters/01/HelloWorld.java.
Search WWH ::




Custom Search