Wiki source code of Short XWiki URLs

Version 49.1 by Vincent Massol on 2015/08/27

Hide last authors
Manuel Smeria 19.4 1 {{box cssClass="floatinginfobox" title="**Contents**"}}
2 {{toc/}}
3 {{/box}}
Jerome 14.1 4
Manuel Smeria 19.4 5 This tutorial shows you how to tune your XWiki platform by replacing the default URL scheme with a shorter scheme.
Jerome 15.1 6
Jerome 14.1 7 {{info}}
8 A short URL is an URL without the ##xwiki/bin/view## parts.
9 {{/info}}
maalej 2.1 10
Dmitry Bakbardin 20.1 11 = I. Application name =
maalej 1.1 12
Jerome 14.1 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##).
maalej 1.1 14
Vincent Massol 47.1 15 == Deploying as ROOT ==
16
Vincent Massol 46.1 17 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##.
Guillaume Fenollar 43.1 18
Vincent Massol 46.1 19 Achieving this depends on the container, as there's no standard regarding the ROOT application (Refer to your container's documentation for more details).
20
21 Some examples:
22 * 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##).
Vincent Massol 26.1 23 * 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:
24 ** 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.
25 ** Rename the existing ##webapps/xwiki## directory into ##webapps/root##.
26 ** 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.
Vincent Massol 10.2 27
Vincent Massol 47.2 28 In addition, starting with XWiki 6.2.8/6.4.3/7.0 you must tell XWiki that it's deployed as ROOT by setting the ##xwiki.webapppath## to empty in your ##xwiki.cfg## configuration file as in:
Vincent Massol 26.1 29
Vincent Massol 46.1 30 {{code language="none"}}
31 xwiki.webapppath=
32 {{/code}}
33
34 The reason is that XWiki cannot guess the webapp context from the URL in this case. This seemed to work on previous versions but it was actually leading to errors from time to time, depending on what URL was used when doing the first request on the XWiki instance.
35
Vincent Massol 49.1 36 === When using the XWiki Debian Package ===
37
38 * Rename the file ##/etc/tomcat7/Catalina/localhost/xwiki.xml## to ##/etc/tomcat7/Catalina/localhost/ROOT.xml## and change the content from:(((
39 {{code language="xml"}}
40 <Context path="/xwiki" docBase="/usr/lib/xwiki" privileged="true"
41 allowLinking="true" crossContext="true">
42 <!-- make symlinks work in Tomcat -->
43 <Resources className="org.apache.naming.resources.FileDirContext"
44 allowLinking="true" />
45 </Context>
46 {{/code}}
47
48 to:
49
50 {{code language="xml"}}
51 <Context path="/" docBase="/usr/lib/xwiki" privileged="true"
52 allowLinking="true" crossContext="true">
53 <!-- make symlinks work in Tomcat -->
54 <Resources className="org.apache.naming.resources.FileDirContext"
55 allowLinking="true" />
56 </Context>
57 {{/code}}
58 )))
59 * Uncomment ##xwiki.webapppath=## in ##/etc/xwiki/xwiki.cfg##
60 * Change the Apache 2 proxy setup from:(((
61 {{code}}
62 RedirectMatch ^/$ /xwiki/
63
64 ProxyRequests Off
65 <Proxy *>
66 Order deny,allow
67 Allow from all
68 </Proxy>
69 ProxyPreserveHost On
70
71 ProxyPass /xwiki ajp://localhost:8009/xwiki
72 {{/code}}
73
74 to:
75
76 {{code}}
77 ProxyRequests Off
78 <Proxy *>
79 Order deny,allow
80 Allow from all
81 </Proxy>
82 ProxyPreserveHost On
83
84 ProxyPass / ajp://localhost:8009/xwiki/
85 {{/code}}
86 )))
87
Dmitry Bakbardin 20.1 88 = II. Servlet mapping name =
maalej 1.1 89
Vincent Massol 29.1 90 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. Generically speaking, to get rid of ##/bin/##, you need to configure your system so that URLs matching ##/*## are mapped to the Struts Servlet (by default only ##/bin/*## URLs are mapped to the Struts Servlet).
maalej 1.1 91
Vincent Massol 35.1 92 However you need to be careful that the following prefixes do NOT go through the Struts Servlet (see your ##web.xml## to check their current mappings):
Guillaume Fenollar 43.1 93
Vincent Massol 29.1 94 * ##/resources/*## and ##/skins/*##: Statically served resources. These need to be served directly as static resources.
Vincent Massol 35.1 95 * ##/rest/*##: REST resources, served by the XWiki REST Servlet
96 * ##/xmlrpc/*##: XML-RPC resources, served by the XWiki XMLRPC Servlet
Guillaume Fenollar 43.1 97 * ##/resources/~*~*/*.gwtrpc##: GWT-RPC calls, served by the XWiki GWT Servlet
Vincent Massol 35.1 98 * ##/webdav/*##: WebDav calls, served by the XWiki WebDAV Servlet
99 * ##/XWikiService##: Another XWiki GWT Servlet
Vincent Massol 29.1 100 * ##/redirect##: The XWiki Redirect Servlet used to redirect to the home page when no page is specified in the URL
maalej 1.1 101
Vincent Massol 38.1 102 There are various alternate to achieve this:
Dmitry Bakbardin 20.1 103
Vincent Massol 38.1 104 {{toc scope="local"/}}
105
Vincent Massol 37.1 106 {{info}}
107 You might be tempted to configure just your XWiki's ##web.xml## file [[but this won't work>>ShortURLsInvalid]].
108 {{/info}}
109
Vincent Massol 29.1 110 == UrlRewriteFilter ==
111
Guillaume Fenollar 43.1 112 {{info}}
113 This is the simplest solution of all but you'll need XWiki 5.2+ because of [[this issue that was fixed in XWiki 5.2>>http://jira.xwiki.org/browse/XWIKI-9430]].
114 {{/info}}
Vincent Massol 29.1 115
Vincent Massol 36.1 116 "UrlRewriteFilter" is a [[framework offering a Servlet Filter>>http://www.tuckey.org/urlrewrite/]] allowing to rewrite URLs.
117
118 Install steps:
Guillaume Fenollar 43.1 119
Vincent Massol 36.1 120 1. [[Download the JAR>>http://www.tuckey.org/urlrewrite/]] and put it in ##WEB-INF/lib##
121 1. Edit your ##WEB-INF/web.xml## and add the ##<filter>## and ##filter-mapping## definitions [[as documented>>http://www.tuckey.org/urlrewrite/]]
122 1. Drop the following content in a file at ##WEB-INF/urlrewrite.xml##:(((
Jerome 14.1 123 {{code language="xml"}}
Vincent Massol 29.1 124 <?xml version="1.0" encoding="utf-8"?>
125 <!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 4.0//EN"
126 "http://www.tuckey.org/res/dtds/urlrewrite4.0.dtd">
Vincent Massol 42.1 127 <urlrewrite decode-using="null">
Vincent Massol 29.1 128
129 <rule>
130 <note>
131 Ensure that URLs ending with .gwtrpc are not modified.
132 </note>
133 <from>^/(.*)\.gwtrpc$</from>
134 <to type="forward" last="true">-</to>
135 </rule>
136
137 <rule>
138 <note>
Vincent Massol 48.2 139 Ensure that URLs that must not be served by the Struts Servlet are not modified.
Vincent Massol 29.1 140 </note>
Dmitry Bakbardin 44.1 141 <from>^/(bin|resources|skins|rest|webdav|xmlrpc|wiki)/(.*)$</from>
Vincent Massol 29.1 142 <to type="forward" last="true">-</to>
143 </rule>
144
145 <rule>
146 <note>
147 For all other URLs we prepend the "/bin/" prefix so that they get routed to the XWiki Action Servlet.
148 </note>
149 <from>^/(.*)$</from>
150 <to type="forward">/bin/$1</to>
151 </rule>
152
Vincent Massol 32.1 153 <outbound-rule>
154 <note>
Vincent Massol 48.1 155 Rewrite outbound URLs to remove the "/bin" part when there are two paths after it.
Vincent Massol 32.1 156 </note>
157 <from>/bin/(.*)/(.*)$</from>
158 <to>/$1/$2</to>
159 </outbound-rule>
160
Vincent Massol 48.1 161 <outbound-rule>
162 <note>
163 Rewrite outbound URLs to remove the "/bin" part when there's a single path after it.
164 </note>
165 <from>/bin/(.*)$</from>
166 <to>/$1</to>
167 </outbound-rule>
168
169 <outbound-rule>
170 <note>
171 Rewrite outbound URLs to remove the "/bin" part it's the last path.
172 </note>
173 <from>/bin$</from>
174 <to>/</to>
175 </outbound-rule>
176
Vincent Massol 29.1 177 </urlrewrite>
178 {{/code}}
Vincent Massol 36.1 179 )))
Vincent Massol 29.1 180
Anca Luca 45.1 181 **Note**: for debugging of URL Rewrite filter, set the ##logLevel## parameter as described in http://urlrewritefilter.googlecode.com/svn/trunk/src/doc/manual/4.0/index.html#filterparams (using the value ##sysout:DEBUG##, the logs can be read in tomcat logs).
182
Vincent Massol 39.1 183 == Apache ==
Jerome 16.1 184
Vincent Massol 39.1 185 Strategy:
Guillaume Fenollar 43.1 186
187 * Tell Apache that ##/skins## and ##/resources## URLs (except for ##/resources/~*~*/*.gwtrpc## ones) are served statically so that they don't go through the Servlet container
188 * Configure ##web.xml## so that ##/*## URLs go through the Struts Servlet and so that ##/resources/~*~*/*.gwtrpc## URLs go through the GWT Servlet
Vincent Massol 39.1 189 * Tell XWiki to not generate URLs with ##bin## in the path
Sergiu Dumitriu 4.1 190
Vincent Massol 39.1 191 Configuration steps:
Guillaume Fenollar 43.1 192
Vincent Massol 39.1 193 * Setup the following Apache configuration:(((
194 {{code language="apache"}}
Manuel Smeria 19.4 195 Alias /skins /usr/local/xwiki/skins
196 Alias /resources /usr/local/xwiki/resources
Jerome 19.1 197
Vincent Massol 34.1 198 RewriteEngine on
199
200 RewriteRule ^/+skins - [L]
201 RewriteCond %{REQUEST_URI} !\.gwtrpc$
202 RewriteRule ^/+resources($|/.*) - [L]
203
204 RewriteRule .* http://localhost:8080%{REQUEST_URI} [P,L]
205 ProxyPassReverse / http://localhost:8080/
206 {{/code}}
Vincent Massol 39.1 207 )))
Vincent Massol 41.1 208 * Edit ##web.xml## and add:(((
Vincent Massol 39.1 209 {{code language="xml"}}
210 <servlet-mapping>
211 <servlet-name>action</servlet-name>
212 <url-pattern>/*</url-pattern>
213 </servlet-mapping>
214 {{/code}}
215 )))
Vincent Massol 41.2 216 * Edit ##web.xml## and replace the existing mapping:(((
Vincent Massol 39.1 217 {{code language="xml"}}
218 <servlet-mapping>
219 <servlet-name>gwtrpc</servlet-name>
220 <url-pattern>*.gwtrpc</url-pattern>
221 </servlet-mapping>
222 {{/code}}
Vincent Massol 34.1 223
Vincent Massol 41.2 224 by
Vincent Massol 34.1 225
Vincent Massol 39.1 226 {{code language="xml"}}
227 <servlet-mapping>
228 <servlet-name>gwtrpc</servlet-name>
229 <url-pattern>/resources/*</url-pattern>
230 </servlet-mapping>
231 {{/code}}
232 )))
233 * Add the following in ##xwiki.cfg## (empty value after the equal sign):(((
Jerome 14.1 234 {{code language="none"}}
Vincent Massol 39.1 235 xwiki.defaultservletpath=
Jerome 14.1 236 {{/code}}
Guillaume Fenollar 43.1 237 )))
Vincent Massol 39.1 238 * {{info}}Only for XWiki 5.2+{{/info}} Add the following in ##xwiki.properties## (empty value after the equal sign):(((
239 {{code language="none"}}
Guillaume Fenollar 43.1 240 url.standard.getEntityPathPrefix=
Vincent Massol 39.1 241 {{/code}}
242 )))
PaulHarris 12.1 243
Vincent Massol 39.1 244 {{warning}}
245 There's a regression in XWiki 5.1 that will prevent this to work correctly. To overcome it, we recommend that you upgrade to XWiki 5.2. If you cannot, you could do the following:
246 * Remove the 3 ##xwiki-platform-url-*## JARs you have in your ##WEB-INF/lib## directory and instead add the following:
247 ** http://maven.xwiki.org/releases/org/xwiki/platform/xwiki-platform-url-api/5.2-milestone-2/xwiki-platform-url-api-5.2-milestone-2.jar
248 ** http://maven.xwiki.org/releases/org/xwiki/platform/xwiki-platform-url-container/5.2-milestone-2/xwiki-platform-url-container-5.2-milestone-2.jar
249 ** http://maven.xwiki.org/releases/org/xwiki/platform/xwiki-platform-url-standard/5.2-milestone-2/xwiki-platform-url-standard-5.2-milestone-2.jar
250 * Edit your ##xwiki.properties## file and add (no value after the equal sign):(((
Dmitry Bakbardin 23.1 251 {{code}}
Vincent Massol 39.1 252 url.standard.entityPathPrefix=
Dmitry Bakbardin 23.1 253 {{/code}}
Vincent Massol 39.1 254 )))
255 {{/warning}}
Dmitry Bakbardin 23.1 256
Vincent Massol 39.1 257 == Others ==
Dmitry Bakbardin 24.1 258
Vincent Massol 39.1 259 Some XWiki users have contributed other solutions but they've not been verified by a XWiki Development Team member at this stage so use at your own risk ;)
Dmitry Bakbardin 24.1 260
Vincent Massol 39.1 261 * [[Lighttpd + Jetty>>ShortURLsLighttpdJetty]]
PaulHarris 12.1 262
Dmitry Bakbardin 20.1 263 = III. Struts action name =
Jerome 14.1 264
Manuel Smeria 19.4 265 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.
266
267 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}}.
268
Dmitry Bakbardin 20.1 269 = IV. Error Page =
Manuel Smeria 19.4 270
Jerome 14.1 271 At the ##WEB-INF/web.xml##, the ##location## of the 404 error code needs to be changed accordingly. For example:
272
273 {{code language="xml"}}
274 <error-page>
mingfai 9.1 275 <error-code>404</error-code>
276 <!--<location>/xwiki/bin/view/Main/DocumentDoesNotExist</location>-->
277 <location>/bin/Main/DocumentDoesNotExist</location>
278 </error-page>
Jerome 14.1 279 {{/code}}
mingfai 9.1 280
Dmitry Bakbardin 20.1 281 = V. Conclusion =
Sergiu Dumitriu 4.1 282
283 After performing all these changes, you should be able to access documents with URLs like:
Jerome 14.1 284
Sergiu Dumitriu 4.1 285 * server.com/Space/Document
286 * server.com/Space/ (pointing to Space.WebHome)
287 * server.com/Document (pointing to Main.Document)
288 * server.com/ will show Main.WebHome, without any redirect.
289
Vincent Massol 5.1 290 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