DekGenius.com
[ Team LiB ] Previous Section Next Section

Recipe 5.12 Rewriting Path Information to CGI Arguments

Problem

You want to pass arguments as part of the URL but have these components of the URL rewritten as CGI QUERY_STRING arguments.

Solution

This is just an example, of course; make appropriate changes to the RewriteRule line to fit your own environment and needs:

RewriteEngine on
RewriteRule ^/book/([^/]*)/([^/]*) /cgi-bin/book.cgi?author=$1&subject=$2 [PT]

Discussion

One reason you might want or need to do this is if you're gluing together two legacy systems that do things in different ways, such as a client application and a vendor script.

For example, the RewriteRule in the Solution will cause:

http://www.example.com/book/apache/bowen

to be rewritten as:

http://www.example.com/cgi-bin/book.cgi?subject=apache&author=bowen

The [PT] flag on the RewriteRule directive instructs Apache to keep processing the URL even after it has been modified; without the flag, the server would directly try to treat the rewritten URL as a filename, instead of continuing to the step at which it determines it's a CGI script. It also allows multiple RewriteRule directives to make additional refinements to the URL.

If the URL being rewritten already has a query string, or might, change the [PT] to [QSA,PT]. The QSA means "query string add" and will cause the query string generated by the rewrite to be added to the query string in the original URL. Without QSA, the original query string will be replaced.

See Also

    [ Team LiB ] Previous Section Next Section