Saturday, August 13, 2011

Apache's mod_rewrite and changing URL cases...

Suppose we want to use Apache's mod_rewrite RewriteMap:

http://example.com/id to http://example.com/ID

RewriteMap uppercase int:toupper
RewriteRule [a-z] %{uppercase:%{REQUEST_URI}} [L,R=301]
The list of internal functions are listed here:
http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#mapfunc
Internal Function
MapType: int, MapSource: Internal Apache function
Here, the source is an internal Apache function. Currently you cannot create your own, but the following functions already exist:

toupper:
Converts the key to all upper case.
tolower:
Converts the key to all lower case.
escape:
Translates special characters in the key to hex-encodings.
unescape:
Translates hex-encodings in the key back to special characters.

You can also use this approach to map static content to a list of servers:
Example:

Rewrite map file

##
##  map.txt -- rewriting map
##

static   www1|www2|www3|www4
dynamic  www5|www6
Configuration directives

RewriteMap servers rnd:/path/to/file/map.txt

RewriteRule ^/(.*\.(png|gif|jpg)) http://${servers:static}/$1 [NC,P,L]
RewriteRule ^/(.*) http://${servers:dynamic}/$1 [P,L]
Hash File

No comments:

Post a Comment