Advanced Proxies

If you need total control of URL transformations, mod_rewrite lets you assign an external script to handle it.

RewriteMap  lb prg:/var/www/rewrite.pl
RewriteRule ^/ysearch/(.*) ${lb:$1,%{QUERY_STRING}} [P]

The RewriteMap directive assigns a server script that handles the URL rewriting. The RewriteRule directive tells the web server to use that server script any time a URL matches the pattern ^/ysearch/(.*).

The second part of the directive takes a little explaining. lb is a reference to the server script we assigned. $1 and %{QUERY_STRING} are values that will be sent to the server script. $1 will contain the part of the URL that matches the regular expression in parentheses in the search pattern (the (.*) part of /ysearch/(.*)). In regular expression parlance the dot character (“.”) matches any character. In regular expressions the asterisk character is a modifier that affects the character that precedes it. It means “0 or more occurrences”. So “.*” is a regular expression that says “match 0 or more of any character.”

Using our earlier example:

/ysearch/webSearch?query=ajax%20javascript

The (.*) part of our pattern matches “webSearch.” This will be the first piece of information sent to our server script. You might be wondering why the pattern doesn’t match “webSearch?query=ajax%20javascript.” That’s because the web server handles the parameters separately.

The %{QUERY_STRING} part of the directive contains those parameters, and it’s the second piece of information ...

Get Ajax and Web Services now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.