Wiki source code of ShortURLs

Version 13.1 by PaulHarris on 2010/12/10

Show last authors
1 #startfloatingbox()
2 *Content*
3 #toc("" "" "")
4 #endfloatingbox()
5
6 1 Short XWiki URLs
7
8 This tutorial shows how to tune your XWiki platform by replacing the default URL scheme with a shorter scheme.
9
10 #info("A short URL is an URL without the <tt>xwiki/bin/view</tt> parts.")
11
12 1.1 Application name
13
14 The <tt>/xwiki/</tt> part of the URL is the application name. It identifies the application that should process the request, and it allows a container to host more than one application. To change it you must refer to your container's documentation and find how to map the context path of a web application. For example on Tomcat it's enough to simply deploy the XWiki webapp in the <tt>webapps</tt> directory, in a sub directory named after the application name you wish to use (e.g. <tt>webapps/myappname</tt>).
15
16 A special case is when deploying XWiki as the ROOT application, which actually allows the application name part to be empty, so an URL can take the form <tt>server.com/bin/view/Space/Document</tt>. Achieving this again depends on the container, as there's no standard regarding the ROOT application. For example in Tomcat, with the default configuration, all it takes is to deploy the XWiki web application in <tt>webapps</tt>, in a sub directory named <tt>ROOT</tt> (i.e. <tt>webapps/ROOT</tt>). In Jetty, the default name is <tt>root</tt>. Refer to your container's documentation for more details.
17
18 #warning("Before XWiki Platform 1.3, the the <tt>display-name</tt> parameter in <tt>web.xml</tt> must also be changed to reflect the new application name. For example if you've deployed the XWiki webapp as ROOT then you'll need to have:
19 {code:xml}<display-name></display-name>{code}
20 ")
21
22 1.1 Struts action name
23
24 The third part, /view/, identifies the struts action that should process a request. So this tells what do we want to do with the document, /view/ it, /edit/ it or /delete/ it, for example. The XWiki platform allows this part to be missing, considering that the default action is to just display the document, so an URL like <tt>server.com/bin/Space/Document</tt> will work out of the box.
25
26 Even more, the URL factory, the component that generates URLs, can be configured to skip this part when the action is /view/. To do this, in <tt>xwiki.cfg</tt> write:
27
28 {code:none}xwiki.showviewaction=0{code}
29
30 1.1 Servlet mapping name
31
32 The second part is the hardest part to remove. It identifies the servlet that should process the page, which, for /bin/, is the Struts servlet. To get rid of /bin/, for the moment <tt>web.xml</tt> must be changed in a
33 container-dependent way, so that the container's default servlet is configured to serve the existing directories, like skins, yui, tinymce and wikieditor.
34
35
36 1.1.1 Original Instructions
37
38 In Jetty, the container shipped with the XWiki installer, you will have to write something like:
39
40 {code:xml}
41 <servlet>
42 <servlet-name>defaultSkins</servlet-name>
43 <servlet-class>org.mortbay.jetty.servlet.DefaultServlet</servlet-class>
44 <init-param>
45 <param-name>relativeResourceBase</param-name>
46 <param-value>skins</param-value>
47 </init-param>
48 <load-on-startup>1</load-on-startup>
49 </servlet>
50 <servlet-mapping>
51 <servlet-name>defaultSkins</servlet-name>
52 <url-pattern>/skins/*</url-pattern>
53 </servlet-mapping>
54 {code}
55
56
57
58 In Tomcat, the default servlet does not accept a parameter for changing the resource base, so you will need to write another default servlet.
59
60 The second thing to do, is to copy the mapping for the Struts servlet to also be activated for /, like:
61 {code:xml}
62 <servlet-mapping>
63 <servlet-name>action</servlet-name>
64 <url-pattern>/*</url-pattern>
65 </servlet-mapping>
66 {code}
67 Be sure to leave the other mappings in place, so that /bin/ works, too.
68
69 And the last thing that must be changed is the default mapping used by the URL factory, by adding in <tt>xwiki.cfg</tt>:
70 {code:none}xwiki.defaultservletpath={code}
71
72 #warning("Before 1.7, the setting name was <tt>xwiki.defaultactionpath</tt>.")
73
74 1.1.1 Specific Lighttpd + Jetty Instructions
75
76 The original instructions MIGHT work for you, if you don't use the WYSIWYG editor. It did not work for me, this is what I had to do.
77
78 The problem is that you need to use Java Struts for the routing. They are not very powerful when it comes to the servlet-mapping configuration. We need to map:
79
80 * /resources/* --> static content
81 * /skins/* --> static content
82 * *.gwtrpc --> a servlet
83 * everything else --> other servlets
84
85 The problem is that .gwtrpc files are "in" the /resources folder, and as the /resources/* mapping will always be a higher priority than *.gwtrpc due to the way structs works.
86
87 So, we have to cheat a bit, and offload the static content to the webserver, which DOES have a powerful route-map configuration.
88
89
90 I use lighttpd, but I assume it can be done in other webservers.
91 This is the configuration I used in the lighttpd config, note that my xwiki folder has been moved to /usr/share/jetty/webapps/root (no 'xwiki' at all):
92
93 {code:none}
94 $HTTP["host"] =~ "^www\.domain\.com$" {
95 # ensure all requests for .gwtrpc files go through to java server
96 # we can put this rule first as a higher priority, which java couldn't do
97 $HTTP["url"] =~ "\.gwtrpc$" {
98 proxy.server = ( "" => (( "host" => "127.0.0.1", "port" => 8080 )))
99 }
100 # otherwise, we can handle the static resources
101 else $HTTP["url"] =~ "^/resources/" {
102 alias.url += ( "/resources" => "/usr/share/jetty/webapps/root/resources" )
103 }
104 # otherwise, we can handle the static resources
105 else $HTTP["url"] =~ "^/skins/" {
106 alias.url += ( "/skins" => "/usr/share/jetty/webapps/root/skins" )
107 }
108 # and here is the primary server
109 else $HTTP["host"] =~ "^www\.domain\.com$" {
110 proxy.server = ( "" => (( "host" => "127.0.0.1", "port" => 8080 )))
111 }
112 }
113 # redirect anything.domain.com to www.domain.com
114 else $HTTP["host"] =~ "\.domain\.com$" {
115 url.redirect = ( "^/(.*)" => "http://www.domain.com/$1" )
116 server.name = "www.domain.com"
117 }
118 # redirect domain.com to www.domain.com
119 else $HTTP["host"] =~ "domain\.com$" {
120 url.redirect = ( "^/(.*)" => "http://www.domain.com/$1" )
121 server.name = "www.domain.com"
122 }
123 {code}
124
125 So lighttpd will serve any static content UNLESS it has .gwtrpc on the end of the URL.
126
127 Then in xeb.xml, I change the gwtrpc mapping to:
128 {code:xml}
129 <servlet-mapping>
130 <servlet-name>gwtrpc</servlet-name>
131
132 <url-pattern>/resources/*</url-pattern>
133 <url-pattern>/skins/*</url-pattern>
134 </servlet-mapping>
135 {code}
136
137 Since we are using a url-pattern of /path/, it will be specific enough to be a higher priority than the / pattern we'll use next. And since the only thing that will come through via resources or skins will be gwtrpc, then we can be sure its ok. Note that only resources is required, but I did both anyway.
138
139 Now, as described above, add a rule to catch everything else and redirect it to our xwiki servlet.
140 {code:xml}
141 <servlet-mapping>
142 <servlet-name>action</servlet-name>
143 <url-pattern>/*</url-pattern>
144 </servlet-mapping>
145 {code}
146
147 And the last thing that must be changed is the default mapping used by the URL factory, by adding in <tt>xwiki.cfg</tt>:
148 {code:none}xwiki.defaultservletpath={code}
149
150
151
152 1.1.1 Alternative: Changing the mapping name
153
154 If removing the <tt>/bin</tt> part is not possible in your environment, you can still rename it to something less technical, and which would fit better in your site, like <tt>/wiki</tt>. To do this, you must first add a mapping for the new path, as in:
155
156 {code:xml}
157 <servlet-mapping>
158 <servlet-name>action</servlet-name>
159 <url-pattern>/wiki/*</url-pattern>
160 </servlet-mapping>
161 {code}
162
163 This means that the wiki now accepts requests through this mapping.
164
165 #info('This specific mapping (<tt>/wiki</tt>) should already be there, but you need to add a new one for other custom mappings.')
166
167 #warning('The <tt>/wiki</tt> mapping is reserved for multiwiki with path based wiki mapping setup, so use something else in this scenarion. It should work fine when multiwikis are disabled, or when only hostname wiki mapping is used.')
168
169 Then you must make sure that accessing the application without a servlet in the path (as in <tt>http://server.com/xwiki/</tt> when XWiki is not set as the ROOT application, or <tt>http://server.com/</tt> if XWiki is the ROOT application) redirects to the right servlet. This involves changing the configuration for the <tt>redirect</tt> servlet in web.xml; search for the declaration of the <tt>redirectHomeServlet</tt>, uncomment the <tt>init-param</tt> section, and adjust accordingly:
170
171 {code:xml}
172 <servlet>
173 <servlet-name>redirectHomeServlet</servlet-name>
174 <servlet-class>com.xpn.xwiki.web.HomePageRedirectServlet</servlet-class>
175 <!-- Uncomment and edit this if you want to redirect to a different home page, or if you have different mappings.
176 Note: the URL should not start with /, because it allows the context name to be changed. If it starts with /,
177 then it should be an absolute URL, including the application context path. -->
178 <init-param>
179 <description>The address to redirect to when the client hits the root of the application.</description>
180 <param-name>homePage</param-name>
181 <param-value>wiki/</param-value>
182 </init-param>
183 </servlet>
184 {code}
185
186 Also change the default mapping used by the URL factory, by adding in xwiki.cfg:
187
188 {code:none}
189 xwiki.defaultservletpath=wiki/
190 {code}
191
192 Optionally, you can make sure that accessing the hostname without a path (as in <tt>http://server.com/</tt>) redirects to the right servlet. This depends on your environment. In a Tomcat + Apache HTTPD + mod_redirect, just update the settings:
193
194 {code:none}
195 RedirectMatch ^/$ /xwiki/wiki/
196 RedirectMatch ^/xwiki/$ /xwiki/wiki/
197 {code}
198
199 In the default standalone distribution (with Jetty), the ROOT application only redirects to the <tt>/xwiki</tt> application, so configuring the XWiki redirect servlet is enough if you don't change the application name. If you do, just edit the <tt>web.xml</tt> of the <tt>root</tt> webapp, uncomment the <tt>init-param</tt> of the <tt>XWikiDispatcherServlet</tt> and change the application name.
200
201 1.1 Error Page
202 At the <tt>WEB-INF/web.xml</tt>, the <tt>location</tt> of the 404 error code needs to be changed accordingly. For example:
203 {code:xml}
204 <error-page>
205 <error-code>404</error-code>
206 <!--<location>/xwiki/bin/view/Main/DocumentDoesNotExist</location>-->
207 <location>/bin/Main/DocumentDoesNotExist</location>
208 </error-page>
209 {code}
210
211 1.1 Conclusion
212
213 After performing all these changes, you should be able to access documents with URLs like:
214 * server.com/Space/Document
215 * server.com/Space/ (pointing to Space.WebHome)
216 * server.com/Document (pointing to Main.Document)
217 * server.com/ will show Main.WebHome, without any redirect.
218
219 As a bonus, these changes are backwards compatible, meaning that any currently working URL will also work with these changes performed, so you won't have any broken bookmarks.

Get Connected