E-Book, Englisch, 160 Seiten
Bowen The Definitive Guide to Apache mod_rewrite
1. ed
ISBN: 978-1-4302-0122-9
Verlag: Apress
Format: PDF
Kopierschutz: 1 - PDF Watermark
E-Book, Englisch, 160 Seiten
ISBN: 978-1-4302-0122-9
Verlag: Apress
Format: PDF
Kopierschutz: 1 - PDF Watermark
* 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.
Autoren/Hrsg.
Weitere Infos & Material
1;Contents at a Glance;5
2;Contents;7
3;About the Author;13
4;Acknowledgments;14
5;Introduction;15
5.1;Who This Book Is For;15
5.2;How This Book Is Structured;15
5.3;Prerequisites;16
5.4;Downloading the Code;16
5.5;Contacting the Author;16
6;1 An Introduction to mod_ rewrite;17
6.1;When to Use mod_ rewrite;17
6.1.1;“ Clean” URLs;18
6.1.2;Mass Virtual Hosting;18
6.1.3;Site Rearrangement;19
6.1.4;Conditional Changes;19
6.1.5;Other Stuff;19
6.2;When Not to Use mod_ rewrite;20
6.2.1;Simple Redirection;20
6.2.2;More Complicated Redirects;21
6.2.3;Virtual Hosts;22
6.2.4;Other Stuff;22
6.3;Summary;22
7;2 Regular Expressions;23
7.1;The Building Blocks;23
7.1.1;Matching Anything (.);25
7.1.2;Escaping Characters (\);25
7.1.3;Anchoring Text to the Start and End (^ and $);25
7.1.4;Matching One or More Characters (+);26
7.1.5;Matching Zero or More Characters (*);26
7.1.6;Greedy Matching;27
7.1.7;Making a Match Optional (?);27
7.1.8;Grouping and Capturing ( () );27
7.1.9;Matching One of a Group of Characters ([ ]);29
7.1.10;Negation (!);29
7.2;Regex Examples;30
7.2.1;Email Address;30
7.2.2;Phone Number;31
7.2.3;Matching URIs;32
7.3;Regex Tools;34
7.3.1;Rebug;35
7.3.2;Regex Coach;36
7.4;Summary;36
8;3 Installing and Configuring mod_ rewrite;37
8.1;Third- Party Distributions;37
8.2;Installing mod_ rewrite;38
8.2.1;Static vs. Shared Objects;38
8.2.2;Installing from Source: Static;39
8.2.3;Installing from Source: Shared;39
8.2.4;Enabling mod_rewrite: Binary Installation;41
8.2.5;Testing Whether mod_rewrite Is Correctly Installed;43
8.3;If You’re Not the System Administrator;44
8.4;Enabling the RewriteLog;45
8.5;Summary;46
9;4 The RewriteRule Directive;47
9.1;Introducing RewriteRule;47
9.1.1;RewriteRule Syntax;48
9.1.2;RewriteRule Context;48
9.1.3;Rewrite Target;51
9.1.4;RewriteRule Flags;53
9.2;Summary;62
10;5 The RewriteCond Directive;63
10.1;RewriteCond Syntax;63
10.1.1;RewriteCond Variables;64
10.1.2;Time-Based Redirection;66
10.1.3;RewriteCond Additional Variables;68
10.1.4;Image Theft;69
10.1.5;RewriteCond Pattern;69
10.1.6;Examples;70
10.1.7;RewriteCond Modifier Flags;71
10.2;Looping;72
10.3;Summary;73
11;6 The RewriteMap Directive;75
11.1;RewriteMap Syntax;75
11.2;Map Types;75
11.2.1;txt Map Files;76
11.2.2;Randomized Rewrites;78
11.2.3;Hash-Type Maps;80
11.2.4;External Programs;82
11.2.5;Internal Functions;83
11.3;Summary;83
12;7 Basic Rewrites;85
12.1;Adjusting URLs;85
12.1.1;Problem: We Want to Rewrite Path Information to a Query String ( Example 1);85
12.1.2;Problem: We Want to Rewrite Path Information to a Query String ( Example 2);86
12.1.3;Problem: We Want to Rewrite Path Information to a Query String ( Example 3);87
12.1.4;Problem: We Have More Than Nine Arguments;88
12.2;Renaming and Reorganization;89
12.2.1;Problem: We’ve Switched from ColdFusion to PHP, but We Want All Old URLs to Continue Working;89
12.2.2;Problem: We’re Looking in More Than One Place for a File;90
12.2.3;Problem: Some of Our Content Is on Another Server;91
12.2.4;Problem: We Require a Canonical Hostname;91
12.2.5;Problem: We’re Viewing the Wrong SSL Host;92
12.2.6;Problem: We Need to Force SSL;93
12.3;Summary;93
13;8 Conditional Rewrites;95
13.1;Looping;95
13.2;Date- and Time- Based Rewrites;97
13.2.1;Problem: We Want to Show a Competition Website Only During a Competition;97
13.3;Redirecting Based on Client Conditions;99
13.3.1;Problem: We Want to Redirect Users Based on Their Browser Type;99
13.3.2;Problem: We Want to Send External Users Elsewhere;100
13.3.3;Problem: We Want to Serve Different Content Based on the User’s Username;100
13.3.4;Problem: We Want to Force Users to Come Through the Front Door;101
13.3.5;Problem: We Want to Prevent Users from Uploading PHP Files to an Unload Area and Then Executing Them;102
13.3.6;Problem: The Client Certificate Validation Error Message Is Indecipherable;103
13.4;Summary;103
14;9 Access Control;105
14.1;When Not to Use mod_ rewrite;105
14.1.1;Address-Based Access Control;105
14.1.2;Environment Variable–Based Access Control;106
14.2;Access Control with mod_ rewrite;107
14.2.1;Problem: We Want to Deny Access to a Particular Directory;107
14.2.2;Problem: We Want to Deny Access to Several Directories at Once;109
14.3;Simple Client- Based Access Control;110
14.3.1;Problem: We Want to Block a Spider from Hammering Our Website;110
14.3.2;Problem: We Want to Prevent “Image Theft”;111
14.4;Summary;113
15;10 Virtual Hosts;115
15.1;Virtual Hosts the Old- Fashioned Way;115
15.2;Configuring Virtual Hosts with mod_ vhost_ alias;117
15.2.1;www.example.com works, but example.com Doesn’t;118
15.2.2;There Are Too Many Directories;119
15.2.3;This Approach Breaks My Other Virtual Hosts;120
15.2.4;Logging;120
15.2.5;It’s Too Inflexible;120
15.3;Mass Virtual Hosting with mod_ rewrite;120
15.3.1;Rewriting Virtual Hosts;121
15.3.2;Virtual Hosts with RewriteMap;124
15.4;Logging for Mass Virtual Hosts;125
15.4.1;Splitting the Log File;126
15.4.2;Using Piped Log Handlers;126
15.5;Summary;127
16;11 Proxying;129
16.1;Proxy Rewrite Rules;129
16.2;Security;130
16.2.1;Apache 1.3;131
16.2.2;Apache 2.0;131
16.3;Proxying Without mod_ rewrite;132
16.4;Proxying with mod_ rewrite;133
16.4.1;Proxying a Particular File Type;133
16.4.2;Proxying to an Application Server;134
16.4.3;Modifying Proxied Content;134
16.4.4;Excluding Content from the Proxy;135
16.4.5;Looking Somewhere Else;136
16.5;Summary;137
17;12 Debugging;139
17.1;RewriteLog;139
17.1.1;A Simple RewriteLog Example;140
17.1.2;Loop Avoidance;142
17.1.3;RewriteRule in .htaccess Files;144
17.2;Regex Building Tools;146
17.3;Summary;148
18;Appendix Additional Resources;149
18.1;Online Resources;149
18.2;Books;149
18.3;PCRE Documentation;150
19;Index;151
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/




