Wiki source code of Short XWiki URLs

Version 26.2 by Vincent Massol on 2013/08/02

Show last authors
1 {{box cssClass="floatinginfobox" title="**Contents**"}}
2 {{toc/}}
3 {{/box}}
4
5 This tutorial shows you how to tune your XWiki platform by replacing the default URL scheme with a shorter scheme.
6
7 {{info}}
8 A short URL is an URL without the ##xwiki/bin/view## parts.
9 {{/info}}
10
11 = I. Application name =
12
13 The ##/xwiki/## 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 ##webapps## directory, in a sub directory named after the application name you wish to use (e.g. ##webapps/myappname##).
14
15 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 ##server.com/bin/view/Space/Document##. Achieving this depends on the container, as there's no standard regarding the ROOT application. For example:
16 * in Tomcat, with the default configuration, all it takes is to deploy the XWiki web application in ##webapps##, in a sub directory named ##ROOT## (i.e. ##webapps/ROOT##).
17 * In Jetty, with the default configuration, all it takes is to deploy the XWiki web application in ##webapps##, in a sub directory named ##root##. Note that if you're using the Standalone distribution (which packages Jetty and HSQLDB) then you'll also need to:
18 ** Remove the existing ##webapps/root## directory which contains a redirect Servlet that automatically redirects root URLs to the ##xwiki## context. You won't need that anymore.
19 ** Rename the existing ##webapps/xwiki## directory into ##webapps/root##.
20 ** Remove the ##jetty/contexts/xwiki.xml## file and thus keep only the ##jetty/contexts/root.xml## file. Otherwise you'll get a warning in the console.
21
22 Refer to your container's documentation for more details.
23
24 = II. Servlet mapping name =
25
26 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 ##web.xml## must be changed in a container-dependent way, so that the container's default servlet is configured to serve the existing directories, like skins, yui, tinymce and wikieditor.
27
28 == 2.1. Original Instructions ==
29
30 === 2.1.1. Container setup ===
31
32 In Jetty, the container shipped with the XWiki installer, you will have to write something like:
33
34 {{code language="xml"}}
35 <servlet>
36 <servlet-name>defaultSkins</servlet-name>
37 <servlet-class>org.mortbay.jetty.servlet.DefaultServlet</servlet-class>
38 <load-on-startup>1</load-on-startup>
39 </servlet>
40 <servlet-mapping>
41 <servlet-name>defaultSkins</servlet-name>
42 <url-pattern>/skins/*</url-pattern>
43 </servlet-mapping>
44 <servlet-mapping>
45 <servlet-name>defaultSkins</servlet-name>
46 <url-pattern>/resources/*</url-pattern>
47 </servlet-mapping>
48 {{/code}}
49
50 {{warning}}
51 If you are using Jetty 1.7 or higher, the correct servlet-class is ##org.eclipse.jetty.servlet.DefaultServlet##.
52 {{/warning}}
53
54 In Tomcat, the default servlet does not accept a parameter for changing the resource base, so you will need to write another default servlet.
55
56 === 2.1.2. Container setup alternative: front-end by-pass ===
57
58 Alternatively, you could by-pass the servlet container at the web front-end level. For example, if you are using Apache httpd as a front-end, and assuming a webapp deployed as a ROOT webapp and an AJP connection between httpd and the servlet container, the following configuration allows you to serve skin files and static resources directly from httpd:
59
60 {{code}}
61 Alias /skins /usr/local/xwiki/skins
62 Alias /resources /usr/local/xwiki/resources
63 ProxyPass /skins/ !
64 ProxyPass /resources/ !
65 {{/code}}
66
67 == 2.2. Struts servlet mapping ==
68
69 The second thing to do is to copy the mapping for the Struts servlet to also be activated for /, like:
70
71 {{code language="xml"}}
72 <servlet-mapping>
73 <servlet-name>action</servlet-name>
74 <url-pattern>/*</url-pattern>
75 </servlet-mapping>
76 {{/code}}
77
78 Be sure to leave the other mappings in place, so that ##/bin/## works, too.
79
80 The last thing that must be changed is the default mapping used by the URL factory, by adding this piece of code in ##xwiki.cfg##: {{code language="none"}}xwiki.defaultservletpath={{/code}}.
81
82 == 2.3. Specific Lighttpd + Jetty Instructions ==
83
84 The original instructions might work for you if you don't use the WYSIWYG editor. It did not work for me and this is what I had to do:
85
86 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:
87
88 * /resources/* ~-~-> static content
89 * /skins/* ~-~-> static content
90 * *.gwtrpc ~-~-> a servlet
91 * everything else ~-~-> other servlets
92
93 The problem is that .gwtrpc files are in the ##/resources/## folder, and the ##/resources/*## mapping will always have a higher priority than *.gwtrpc due to the way structs works.
94
95 So, we have to cheat a bit, and offload the static content to the webserver, which does have a powerful route-map configuration.
96
97 I used lighttpd, but I assume it can be done with other webservers too. 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)):
98
99 {{code language="none"}}
100 $HTTP["host"] =~ "^www\.domain\.com$" {
101 # ensure all requests for .gwtrpc files go through to java server
102 # we can put this rule first as a higher priority, which java couldn't do
103 $HTTP["url"] =~ "\.gwtrpc$" {
104 proxy.server = ( "" => (( "host" => "127.0.0.1", "port" => 8080 )))
105 }
106 # otherwise, we can handle the static resources
107 else $HTTP["url"] =~ "^/resources/" {
108 alias.url += ( "/resources" => "/usr/share/jetty/webapps/root/resources" )
109 }
110 # otherwise, we can handle the static resources
111 else $HTTP["url"] =~ "^/skins/" {
112 alias.url += ( "/skins" => "/usr/share/jetty/webapps/root/skins" )
113 }
114 # and here is the primary server
115 else $HTTP["host"] =~ "^www\.domain\.com$" {
116 proxy.server = ( "" => (( "host" => "127.0.0.1", "port" => 8080 )))
117 }
118 }
119 # redirect anything.domain.com to www.domain.com
120 else $HTTP["host"] =~ "\.domain\.com$" {
121 url.redirect = ( "^/(.*)" => "http://www.domain.com/$1" )
122 server.name = "www.domain.com"
123 }
124 # redirect domain.com to www.domain.com
125 else $HTTP["host"] =~ "domain\.com$" {
126 url.redirect = ( "^/(.*)" => "http://www.domain.com/$1" )
127 server.name = "www.domain.com"
128 }
129 {{/code}}
130
131 So lighttpd will serve any static content unless it has .gwtrpc on the end of the URL.
132
133 If you use Nginx as a web-server, just add three more locations and set "root" to them. By //try_files// Nginx checks static content presence and if doesn't exist, redirect it to the Tomcat (we expect dynamic content in this case, including all *.gwtrpc requests).
134
135 {{code}}
136 location /skins/ {
137 root /var/lib/tomcat7/webapps/ROOT;
138 }
139
140 location /resources/ {
141 try_files $uri $uri/ @fallback;
142 root /var/lib/tomcat7/webapps/ROOT;
143 }
144
145 location @fallback {
146 proxy_pass http://localhost:8080;
147 }
148
149 {{/code}}
150
151 In the example above XWiki installed as ROOT application in Tomcat. Change path to your XWiki application accordingly.
152
153
154 Then in ##web.xml##, I changed the gwtrpc mapping to:
155
156 {{code language="xml"}}
157 <servlet-mapping>
158 <servlet-name>gwtrpc</servlet-name>
159
160 <url-pattern>/resources/*</url-pattern>
161 <url-pattern>/skins/*</url-pattern>
162 </servlet-mapping>
163 {{/code}}
164
165 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 it's ok. Note that only ##resources## is required, but I did both anyway.
166
167 Now, as described above, add a rule to catch everything else and redirect it to your xwiki servlet:
168
169 {{code language="xml"}}
170 <servlet-mapping>
171 <servlet-name>action</servlet-name>
172 <url-pattern>/*</url-pattern>
173 </servlet-mapping>
174 {{/code}}
175
176 The last thing that must be changed is the default mapping used by the URL factory, by adding this piece of code in ##xwiki.cfg##: {{code language="none"}}xwiki.defaultservletpath={{/code}}.
177
178 == 2.4. Alternative: Changing the mapping name ==
179
180 If removing the ##/bin## part is not possible in your environment, you can still rename it to something less technical, and which would better fit your site, like ##/wiki##. To do this, you must first add a mapping for the new path, as in:
181
182 {{code language="xml"}}
183 <servlet-mapping>
184 <servlet-name>action</servlet-name>
185 <url-pattern>/wiki/*</url-pattern>
186 </servlet-mapping>
187 {{/code}}
188
189 This means that the wiki now accepts requests through this mapping.
190
191 {{info}}
192 This specific mapping (##/wiki##) should already be there, but you need to add a new one for other custom mappings.
193 {{/info}}
194
195 {{warning}}
196 The ##/wiki## mapping is reserved for multiwikis 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.
197 {{/warning}}
198
199 Then you must make sure that accessing the application without a servlet in the path (as in ##http:~/~/server.com/xwiki/## when XWiki is not set as the ROOT application, or ##http:~/~/server.com/## if XWiki is the ROOT application) redirects to the right servlet. This involves changing the configuration for the ##redirect## servlet in ##web.xml##; search for the declaration of the ##redirectHomeServlet##, uncomment the ##init-param## section, and adjust accordingly:
200
201 {{code language="xml"}}
202 <servlet>
203 <servlet-name>redirectHomeServlet</servlet-name>
204 <servlet-class>com.xpn.xwiki.web.HomePageRedirectServlet</servlet-class>
205 <!-- Uncomment and edit this if you want to redirect to a different home page, or if you have different mappings.
206 Note: the URL should not start with /, because it allows the context name to be changed. If it starts with /,
207 then it should be an absolute URL, including the application context path. -->
208 <init-param>
209 <description>The address to redirect to when the client hits the root of the application.</description>
210 <param-name>homePage</param-name>
211 <param-value>wiki/</param-value>
212 </init-param>
213 </servlet>
214 {{/code}}
215
216 Also change the default mapping used by the URL factory, by adding this in ##xwiki.cfg##: {{code language="none"}}xwiki.defaultservletpath=wiki/{{/code}}.
217
218 Optionally, you can make sure that accessing the hostname without a path (as in ##http:~/~/server.com/##) redirects to the right servlet. This depends on your environment. In a Tomcat + Apache HTTPD + mod_redirect, just update these settings:
219
220 {{code language="none"}}
221 RedirectMatch ^/$ /xwiki/wiki/
222 RedirectMatch ^/xwiki/$ /xwiki/wiki/
223 {{/code}}
224
225 In the default standalone distribution (with Jetty), the ROOT application only redirects to the ##/xwiki## application, so configuring the XWiki redirect servlet is enough if you don't change the application name. If you do, just edit the ##web.xml## of the ##root## webapp, uncomment the ##init-param## of the ##XWikiDispatcherServlet## and change the application name.
226
227 = III. Struts action name =
228
229 The third part, ##/view/##, identifies the struts action that should process a request. So this tells what 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 ##server.com/bin/Space/Document## will work out of the box.
230
231 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 write this code in ##xwiki.cfg##: {{code language="none"}}xwiki.showviewaction=0{{/code}}.
232
233 = IV. Error Page =
234
235 At the ##WEB-INF/web.xml##, the ##location## of the 404 error code needs to be changed accordingly. For example:
236
237 {{code language="xml"}}
238 <error-page>
239 <error-code>404</error-code>
240 <!--<location>/xwiki/bin/view/Main/DocumentDoesNotExist</location>-->
241 <location>/bin/Main/DocumentDoesNotExist</location>
242 </error-page>
243 {{/code}}
244
245 = V. Conclusion =
246
247 After performing all these changes, you should be able to access documents with URLs like:
248
249 * server.com/Space/Document
250 * server.com/Space/ (pointing to Space.WebHome)
251 * server.com/Document (pointing to Main.Document)
252 * server.com/ will show Main.WebHome, without any redirect.
253
254 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