Bowen The Definitive Guide to Apache mod_rewrite
1. Auflage 2006
ISBN: 978-1-4302-0122-9
Verlag: APRESS
Format: PDF
Kopierschutz: 1 - PDF Watermark
E-Book, Englisch, 160 Seiten, eBook
ISBN: 978-1-4302-0122-9
Verlag: APRESS
Format: PDF
Kopierschutz: 1 - PDF Watermark
Zielgruppe
Professional/practitioner
Autoren/Hrsg.
Weitere Infos & Material
An Introduction to mod_rewrite.- Regular Expressions.- Installing and Configuring mod_rewrite.- The RewriteRule Directive.- The RewriteCond Directive.- The RewriteMap Directive.- Basic Rewrites.- Conditional Rewrites.- Access Control.- Virtual Hosts.- Proxying.- Debugging.
When Not to Use mod_rewrite (p. 4)
As important as knowing when and how to use mod_rewrite is having a firm grasp on what other tools Apache offers, so that you know when not to use mod_rewrite. All of mod_rewrite’s amazing power comes at the cost of performance. Running regular expressions consumes time and memory, and it’s ideal to avoid it if alternate approaches are available.
However, even when there are one or more alternate approaches, it is seldom the case that one option is clearly the best one to use all the time. There are always a number of factors that you need to consider.
Just as there are several categories in which mod_rewrite use tends to fall, there are also several categories into which common misuse of mod_rewrite falls, as we’ll cover in the following sections.
Simple Redirection
Probably the most common misuse of mod_rewrite is for simple redirection. Redirection is used when a client requests one URL, and we want to give them a different one instead. In many cases, this is a simple one-to-one mapping. That is, it could be a mapping of one URL to another URL, or perhaps one directory to another directory, and sometimes even a mapping of one virtual host to another one, or perhaps to another server entirely.
In each of these cases, the Redirect directive is sufficient. The syntax of the Redirect directive is as follows:
Redirect [Original] [Target]
where [Original] is the URL that was originally requested, and [Target] is the fully qualified URL to which you wish to redirect it. When the user requests the original URL, Apache will send a redirection message back to the browser, which will then request the new URL.
The address appearing in the address bar of the user’s browser will change to the new URL. This approach requires a second round-trip to the web server in order to retrieve the content. The advantage of this approach, in addition to simplicity, is that the new corrected URL is announced to the user (who may or may not notice), but also that an automated process such as a search engine indexer will update its records to reflect the new URL and stop requesting the old one.
Several examples of the Redirect directive follow:
Redirect /index.cfm http://www.example.com/index.php
In this example, only one possible URL is redirected. That is, if someone requests
http://www.example.com/index.cfm, they will be sent instead to
http://www.example.com/
index.php, but no other URLs will be affected.
In this next example, we’ve renamed our /pics/ directory to /images/ instead, and we want all requests for things in /pics/ to go to /images/ instead:
Redirect /pics/ http://www.example.com/images/