[ Pobierz całość w formacie PDF ]
slash, it is interpreted to be a file path relative to the current directory. If it does start with a slash, it is interpreted as an absolute file system path. This can be exceedingly incon- venient in many cases. Two particular cases bear describing. 5610_c04_final.qxd 1/10/06 1:13 AM Page 44 44 CHAPTER 4 % THE REWRITE RULE DIRECTIVE The most common case is when you want to provide a URL path, and it is inconven- ient to provide a full file system path. The more serious case is when the target of a rewrite is a CGI program or other dynamic content. When a URL request is rewritten to a file path, the usual mechanisms for handling this content are bypassed, and the file is served to the client in its raw form: RewriteRule ^/images/(.*)\.gif /usr/local/apache/cgi-bin/images.pl?gif=$1 This example, as given here, has the rather unexpected result of serving unexecuted Perl code to the browser, because it has bypassed the ScriptAliasmechanism, which relies on the URL starting with /cgi-bin/. To get around these cases, the [PT]flag causes the rewrite target to be passed back to the URL mapping engine, so that it is treated like a URL rather than like a file path. The preceding example would become RewriteRule ^/images/(.*)\.gif /cgi-bin/images.pl?gif=$1 [PT] Similarly, whenever Aliasis used to map a URI to a directory, it is very useful to use [PT]so that the rewrite target can use the Aliaspath, rather than the full directory path. Many people, once aware of the [PT]flag, start using it all the time. It tends to be far more convenient than providing file paths, and, as a bonus, it ensures that the target is treated the way that most people expect it to be treated: as a URL. This flag s greater ease of use may make up for what it lacks in efficiency for many server administrators. Query String Append: QSA By default, the RewriteRuledirective ignores QUERY_STRINGarguments. To remedy this deficiency, the Query String Append, or [QSA], flag causes the QUERY_STRINGto be retained exactly as it is and tacked onto the end of the resulting request, in addition to anything that may have been put there during the rewriting of the URL. The default behavior, with- out the [QSA]flag, is to replace the existing QUERY_STRINGwith the new one. Since the use of the ordinary Redirect(and RedirectMatch) directive does not retain QUERY_STRINGarguments at all, it is often useful to use the [QSA]flag in attempts to con- struct redirects that retain these arguments: RewriteRule ^/example.php$ /cgi-bin/index.cgi [PT,QSA] In this case, any QUERY_STRINGarguments sent to the URL /example.phpwill instead be sent to /cgi-bin/index.cgi. 5610_c04_final.qxd 1/10/06 1:13 AM Page 45 CHAPTER 4 % THE REWRITE RULE DIRECTIVE 45 Redirect: R The Redirect, or [R], flag forces an HTTP 302 Redirect to be sent to the client. This means, among other things, that the new URL will appear in the browser s address bar, whereas most of the other uses of RewriteRuleleave the end user completely unaware that any rewriting occurred. Most of the time when people use mod_rewrite to do URL redirection, the Redirect or RedirectMatchdirectives are actually a better choice than RewriteRule. However, there are certainly many cases when doing a redirect via RewriteRuleis appropriate. In this example, a redirect is necessary so that the browser is not confused about the type of the image being served: RewriteRule ^/images/(.*)\.gif /jpegs/$1.jpg [L,R] You will usually want to use an [L]flag in conjunction with the [R]flag, to ensure that the redirection happens immediately and further rewriting does not occur. %Note When the rewrite target is a fully qualified URL (complete with http://), redirection is the default behavior. If you want to have a different redirection status code used, you can specify this as an argument to the [R]flag: RewriteRule ^/images/(.*\.gif) /jpegs/$1.jpg [L,R=301] Skip: S The Skip, or [S], flag causes the rewrite engine to skip the next n rules, when the current rule matches. This may be used to build an if/else structure. The rules skipped become the else clause, since they are skipped when the if clause matches. The motivation for this might become more apparent once the RewriteCond
[ Pobierz całość w formacie PDF ] zanotowane.pldoc.pisz.plpdf.pisz.plkarro31.pev.pl
|