: Rich Bowen
: The Definitive Guide to Apache mod_rewrite
: Apress
: 9781430201229
: 1
: CHF 49.80
:
: Informatik
: English
: 160
: Wasserzeichen
: PC/MAC/eReader/Tablet
: PDF
* The Definitive Guide to Apache mod_rewrite is the first book devoted to mastering mod_rewrite, Apache's most powerful (and confusing) module.

* It is authored by noted Apache expert and Apache Software Foundation member Rich Bowen, who draws on several years of extensive experience administering the Apache server as well as speaking and writing regularly on the topic.

* This book covers all matters pertinent to mod_rewrite: installation and configuration, basic and conditional rewrites, access control, maintaining virtual hosts, and proxying.



Rich Bowen is a world-recognized expert and member of the Apache Software Foundation. He has authored several books on Apache, and regularly contributes to the Apache Server documentation project.

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/

Contents at a Glance5
Contents7
About the Author13
Acknowledgments14
Introduction15
Who This Book Is For15
How This Book Is Structured15
Prerequisites16
Downloading the Code16
Contacting the Author16
1 An Introduction to mod_ rewrite17
When to Use mod_ rewrite17
Clean URLs18
Mass Virtual Hosting18
Site Rearrangement19
Conditional Changes19
Other Stuff19
When Not to Use mod_ rewrite20
Simple Redirection20
More Complicated Redirects21
Virtual Hosts22
Other Stuff22
Summary22
2 Regular Expressions23
The Building Blocks23
Matching Anything (.)25
Escaping Characters (\)25
Anchoring Text to the Start and End (^ and $)25
Matching One or More Characters (+)26
Matching Zero or More Characters (*)26
Greedy Matching27
Making a Match Optional (?)27
Grouping and Capturing ( () )27
Matching One of a Group of Characters ([ ])29
Negation (!)29
Regex Examples30
Email Address30
Phone Number31
Matching URIs32
Regex Tools34
Rebug35
Regex Coach36
Summary36
3 Installing and Configuring mod_ rewrite37
Third- Party Distributions37
Installing mod_ rewrite38
Static vs. Shared Objects38
Installing from Source: Static39
Installing from Source: Shared39
Enabling mod_rewrite: Binary Installation41
Testing Whether mod_rewrite Is Correctly Installed43
If You re Not the System Administrator44
Enabling the RewriteLog45
Summary46
4 The RewriteRule Directive47
Introducing RewriteRule47
RewriteRule Syntax48
RewriteRule Context48
Rewrite Target51
RewriteRule Flags53
Summary62
5 The RewriteCond Directive63
RewriteCond Syntax63
RewriteCond Variables64
Time-Based Redirection66
RewriteCond Additional Variables68
Image Theft69
RewriteCond Pattern69
Examples70
RewriteCond Modifier Flags71
Looping72
Summary73
6 The RewriteMap Directive75
RewriteMap Syntax75
Map Types75
txt Map Files76
Randomized Rewrites78
Hash-Type Maps80
External Programs82
Internal Functions83
Summary83
7 Basic Rewrites85
Adjusting URLs85
Problem: We Want to Rewrite Path Information to a Query String ( Example 1)85
Problem: We Want to Rewrite Path Information to a Query String ( Example 2)86
Problem: We Want to Rewrite Path Information to a Query String ( Example 3)87
Problem: We Have More Than Nine Arguments88
Renaming and Reorganization89
Problem: We ve Switched from ColdFusion to PHP, but We Want All Old URLs to Continue Working89
Problem: We re Looking in More Than One Place for a File90
Problem: Some of Our Content Is on Another Server91
Problem: We Require a Canonical Hostname91
Problem: We re Viewing the Wrong SSL Host92
Problem: We Need to Force SSL93
Summary93
8 Conditional Rewrites95
Looping95
Date- and