Apache HTTP Server Version 2.4
This document supplements the mod_rewrite
reference documentation. It describes
how you can use mod_rewrite
to redirect and remap
request. This includes many examples of common uses of mod_rewrite,
including detailed descriptions of how each works.
DocumentRoot
Assume we have recently renamed the page
foo.html
to bar.html
and now want
to provide the old URL for backward compatibility. However,
we want that users of the old URL even not recognize that
the pages was renamed - that is, we don't want the address to
change in their browser.
We rewrite the old URL to the new one internally via the following rule:
RewriteEngine on RewriteRule "^/foo\.html$" "/bar.html" [PT]
Assume again that we have recently renamed the page
foo.html
to bar.html
and now want
to provide the old URL for backward compatibility. But this
time we want that the users of the old URL get hinted to
the new one, i.e. their browsers Location field should
change, too.
We force a HTTP redirect to the new URL which leads to a change of the browsers and thus the users view:
RewriteEngine on RewriteRule "^/foo\.html$" "bar.html" [R]
In this example, as contrasted to the internal example above, we can simply use the Redirect directive. mod_rewrite was used in that earlier example in order to hide the redirect from the client:
Redirect "/foo.html" "/bar.html"
If a resource has moved to another server, you may wish to have URLs continue to work for a time on the old server while people update their bookmarks.
You can use mod_rewrite
to redirect these URLs
to the new server, but you might also consider using the Redirect
or RedirectMatch directive.
#With mod_rewrite RewriteEngine on RewriteRule "^/docs/(.+)" "http://new.example.com/docs/$1" [R,L]
#With RedirectMatch RedirectMatch "^/docs/(.*)" "http://new.example.com/docs/$1"
#With Redirect Redirect "/docs/" "http://new.example.com/docs/"
How can we transform a static page
foo.html
into a dynamic variant
foo.cgi
in a seamless way, i.e. without notice
by the browser/user.
We just rewrite the URL to the CGI-script and force the
handler to be cgi-script so that it is
executed as a CGI program.
This way a request to /~quux/foo.html
internally leads to the invocation of
/~quux/foo.cgi
.
RewriteEngine on RewriteBase "/~quux/" RewriteRule "^foo\.html$" "foo.cgi" [H=cgi-script]