]> git.evergreen-ils.org Git - working/Evergreen.git/blob - docs/TechRef/Apache/rewrite_tricks.txt
LP#1413621 Docs: Apache Rewrite Tricks
[working/Evergreen.git] / docs / TechRef / Apache / rewrite_tricks.txt
1 Apache Rewrite Tricks
2 ---------------------
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.
4
5 Short URLs
6 ~~~~~~~~~~
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.
8
9 [source,conf]
10 ----
11 # My Account - http://host.ext/myaccount -> My Account Page
12 RewriteRule ^/myaccount https://%{HTTP_HOST}/eg/opac/myopac/main [R]
13
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]
16 ----
17
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.
21
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.
23
24 [source,conf]
25 ----
26 ServerAlias *.example.com
27 ----
28
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.
30
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.
32
33 .Template Toolkit ENV example, link library name/url if set
34 [source,html]
35 ----
36 [% IF ENV.eglibname && ENV.egliburl %]<a href="[% ENV.egliburl %]">[% ENV.eglibname %]</a>[% END %]
37 ----
38
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.
40
41 .Apache Config
42 [source,conf]
43 ----
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}]
52 ----
53
54 .Contents of libid.txt File
55 [source,txt]
56 ----
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
64 ----
65
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:
67
68 .Apache Config
69 [source,conf]
70 ----
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}}]
77 ----
78
79 .Contents of libname.txt File
80 [source,txt]
81 ----
82 # Note that we cannot have spaces in the "value", so instead &#32; is used. &nbsp; is also an option.
83 BRANCH1 Branch&#32;One
84 BRANCH2 Branch&#32;Two
85 BRANCH3 Branch&#32;Three
86 CONS Example&#32;Consortium&#32;Name
87 ----
88
89 .Contents of liburl.txt File
90 [source,txt]
91 ----
92 BRANCH1 http://branch1.example.org
93 BRANCH2 http://branch2.example.org
94 BRANCH3 http://branch3.example.org
95 CONS http://example.org
96 ----
97
98 Or, perhaps set the "physical location" variable for default search/display library:
99
100 .Apache Config
101 [source,conf]
102 ----
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}}]
107 ----
108
109 .Contents of libphysloc.txt File
110 [source,txt]
111 ----
112 BRANCH1 4
113 BRANCH2 5
114 BRANCH3 6
115 CONS 1
116 ----
117
118 Going further, you could also replace files to be downloaded, such as images or stylesheets, on the fly:
119
120 .Apache Config
121 [source,conf]
122 ----
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
128 ----
129
130 Note that template files themselves cannot be replaced in that manner.