3 It is possible to use Apache's Rewrite Module features to perform a number of useful tricks that can make people's lives much easier.
7 Making short URLs for common destinations can simplify making printed media as well as shortening or simplifying what people need to type. These are also easy to add and require minimal maintenance, and generally can be implemented with a single line addition to your eg_vhost.conf file.
11 # My Account - http://host.ext/myaccount -> My Account Page
12 RewriteRule ^/myaccount https://%{HTTP_HOST}/eg/opac/myopac/main [R]
14 # ISBN Search - http://host.ext/search/isbn/<ISBN NUMBER> -> Search Page
15 RewriteRule ^/search/isbn/(.*) /eg/opac/results?_special=1&qtype=identifier|isbn&query=$1 [R]
18 Domain Based Content with RewriteMaps
19 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 One creative use of Rewrite features is domain-based configuration in a single eg_vhost.conf file. Regardless of how many VirtualHost blocks use the configuration you don't need to duplicate things for minor changes, and can in fact use wildcard VirtualHost blocks to serve multiple subdomains.
22 For the wildcard blocks you will want to use a ServerAlias directive, and for SSL VirtualHost blocks ensure you have a wildcard SSL certificate.
26 ServerAlias *.example.com
29 For actually changing things based on the domain, or subdomain, you can use RewriteMaps. Each RewriteMap is generally a lookup table of some kind. In the following examples we will generally use text files, though database lookups and external programs are also possible.
31 Note that in the examples below we generally store things in Environment Variables. From within Template Toolkit templates you can access environment variables with the ENV object.
33 .Template Toolkit ENV example, link library name/url if set
36 [% IF ENV.eglibname && ENV.egliburl %]<a href="[% ENV.egliburl %]">[% ENV.eglibname %]</a>[% END %]
39 The first lookup to do is a domain to identifier, allowing us to re-use identifiers for multiple domains. In addition we can also supply a default identifier, for when the domain isn't present in the lookup table.
44 # This internal map allows us to lowercase our hostname, removing case issues in our lookup table
45 # If you preferr uppercase you can use "uppercase int:toupper" instead.
46 RewriteMap lowercase int:tolower
47 # This provides a hostname lookup
48 RewriteMap eglibid txt:/openils/conf/libid.txt
49 # This stores the identifier in a variable (eglibid) for later use
50 # In this case CONS is the default value for when the lookup table has no entry
51 RewriteRule . - [E=eglibid:${eglibid:${lowercase:%{HTTP_HOST}}|CONS}]
54 .Contents of libid.txt File
57 # Comments can be included
58 # Multiple TLDs for Branch 1
59 branch1.example.com BRANCH1
60 branch1.example.net BRANCH1
61 # Branches 2 and 3 don't have alternate TLDs
62 branch2.example.com BRANCH2
63 branch3.example.com BRANCH3
66 Once we have identifiers we can look other information up, when appropriate. For example, say we want to look up library names and URLs:
71 # Library Name Lookup - Note we provide no default in this case.
72 RewriteMap eglibname txt:/openils/conf/libname.txt
73 RewriteRule . - [E=eglibname:${eglibname:%{ENV:eglibid}}]
74 # Library URL Lookup - Also with no default.
75 RewriteMap egliburl txt:/openils/conf/liburl.txt
76 RewriteRule . - [E=egliburl:${egliburl:%{ENV:eglibid}}]
79 .Contents of libname.txt File
82 # Note that we cannot have spaces in the "value", so instead   is used. is also an option.
83 BRANCH1 Branch One
84 BRANCH2 Branch Two
85 BRANCH3 Branch Three
86 CONS Example Consortium Name
89 .Contents of liburl.txt File
92 BRANCH1 http://branch1.example.org
93 BRANCH2 http://branch2.example.org
94 BRANCH3 http://branch3.example.org
95 CONS http://example.org
98 Or, perhaps set the "physical location" variable for default search/display library:
103 # Lookup "physical location" IDs
104 RewriteMap eglibphysloc txt:/openils/conf/libphysloc.txt
105 # Note: physical_loc is a variable used in the TTOPAC and should not be re-named
106 RewriteRule . - [E=physical_loc:${eglibphysloc:%{ENV:eglibid}}]
109 .Contents of libphysloc.txt File
118 Going further, you could also replace files to be downloaded, such as images or stylesheets, on the fly:
123 # A file exists based on eglibid and the requested file
124 # Say, BRANCH1/opac/images/main_logo.png
125 RewriteCond %{DOCUMENT_ROOT}/%{ENV:eglibid}%{REQUEST_URI} -f
126 # Serve up the eglibid version of the file instead
127 RewriteRule (.*) /%{ENG:eglibid}$1
130 Note that template files themselves cannot be replaced in that manner.