Wiki source code of Short XWiki URLs

Version 60.8 by Ecaterina Moraru (Valica) on 2017/09/06

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:
Paul Libbrecht 51.1 22
Vincent Massol 46.1 23 * 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 24 * 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:
25 ** 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.
26 ** Rename the existing ##webapps/xwiki## directory into ##webapps/root##.
27 ** 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 28
Vincent Massol 47.2 29 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 30
Vincent Massol 46.1 31 {{code language="none"}}
32 xwiki.webapppath=
33 {{/code}}
34
35 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.
36
Vincent Massol 49.1 37 === When using the XWiki Debian Package ===
38
Ecaterina Moraru (Valica) 60.8 39 See [[Installation using Debian (.DEB) packages>>platform:Documentation.AdminGuide.Installation.InstallationViaAPT.WebHome||anchor="HXWikiasrootwebapp28shortURLs29"]].
Vincent Massol 49.1 40
Dmitry Bakbardin 20.1 41 = II. Servlet mapping name =
maalej 1.1 42
Vincent Massol 29.1 43 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 44
Vincent Massol 35.1 45 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 46
Vincent Massol 29.1 47 * ##/resources/*## and ##/skins/*##: Statically served resources. These need to be served directly as static resources.
Vincent Massol 35.1 48 * ##/rest/*##: REST resources, served by the XWiki REST Servlet
Thomas Mortagne 52.1 49 * ##/xmlrpc/*##: XML-RPC resources, served by the XWiki XMLRPC Servlet {{warning}}Not part of XE anymore since 7.3{{/warning}}
Guillaume Fenollar 43.1 50 * ##/resources/~*~*/*.gwtrpc##: GWT-RPC calls, served by the XWiki GWT Servlet
Vincent Massol 35.1 51 * ##/webdav/*##: WebDav calls, served by the XWiki WebDAV Servlet
52 * ##/XWikiService##: Another XWiki GWT Servlet
Vincent Massol 29.1 53 * ##/redirect##: The XWiki Redirect Servlet used to redirect to the home page when no page is specified in the URL
maalej 1.1 54
Vincent Massol 38.1 55 There are various alternate to achieve this:
Dmitry Bakbardin 20.1 56
Vincent Massol 38.1 57 {{toc scope="local"/}}
58
Vincent Massol 37.1 59 {{info}}
60 You might be tempted to configure just your XWiki's ##web.xml## file [[but this won't work>>ShortURLsInvalid]].
61 {{/info}}
62
Vincent Massol 29.1 63 == UrlRewriteFilter ==
64
Guillaume Fenollar 43.1 65 {{info}}
Thomas Mortagne 59.1 66 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>>https://jira.xwiki.org/browse/XWIKI-9430]].
Guillaume Fenollar 43.1 67 {{/info}}
Vincent Massol 29.1 68
Vincent Massol 36.1 69 "UrlRewriteFilter" is a [[framework offering a Servlet Filter>>http://www.tuckey.org/urlrewrite/]] allowing to rewrite URLs.
70
71 Install steps:
Guillaume Fenollar 43.1 72
Vincent Massol 36.1 73 1. [[Download the JAR>>http://www.tuckey.org/urlrewrite/]] and put it in ##WEB-INF/lib##
74 1. Edit your ##WEB-INF/web.xml## and add the ##<filter>## and ##filter-mapping## definitions [[as documented>>http://www.tuckey.org/urlrewrite/]]
75 1. Drop the following content in a file at ##WEB-INF/urlrewrite.xml##:(((
Jerome 14.1 76 {{code language="xml"}}
Vincent Massol 29.1 77 <?xml version="1.0" encoding="utf-8"?>
78 <!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 4.0//EN"
79 "http://www.tuckey.org/res/dtds/urlrewrite4.0.dtd">
Vincent Massol 42.1 80 <urlrewrite decode-using="null">
Vincent Massol 29.1 81
82 <rule>
83 <note>
84 Ensure that URLs ending with .gwtrpc are not modified.
85 </note>
86 <from>^/(.*)\.gwtrpc$</from>
87 <to type="forward" last="true">-</to>
88 </rule>
89
90 <rule>
91 <note>
Vincent Massol 48.2 92 Ensure that URLs that must not be served by the Struts Servlet are not modified.
Vincent Massol 29.1 93 </note>
Vincent Massol 58.2 94 <from>^/((bin|resources|skins|rest|webdav|xmlrpc|wiki|webjars)/(.*)|robots\.txt)$</from>
Vincent Massol 29.1 95 <to type="forward" last="true">-</to>
96 </rule>
97
98 <rule>
99 <note>
100 For all other URLs we prepend the "/bin/" prefix so that they get routed to the XWiki Action Servlet.
101 </note>
102 <from>^/(.*)$</from>
103 <to type="forward">/bin/$1</to>
104 </rule>
105
Vincent Massol 32.1 106 <outbound-rule>
107 <note>
Vincent Massol 48.1 108 Rewrite outbound URLs to remove the "/bin" part when there are two paths after it.
Vincent Massol 32.1 109 </note>
110 <from>/bin/(.*)/(.*)$</from>
111 <to>/$1/$2</to>
112 </outbound-rule>
113
Vincent Massol 48.1 114 <outbound-rule>
115 <note>
116 Rewrite outbound URLs to remove the "/bin" part when there's a single path after it.
117 </note>
118 <from>/bin/(.*)$</from>
119 <to>/$1</to>
120 </outbound-rule>
121
122 <outbound-rule>
123 <note>
124 Rewrite outbound URLs to remove the "/bin" part it's the last path.
125 </note>
126 <from>/bin$</from>
127 <to>/</to>
128 </outbound-rule>
129
Vincent Massol 29.1 130 </urlrewrite>
131 {{/code}}
Vincent Massol 36.1 132 )))
Vincent Massol 29.1 133
Anca Luca 45.1 134 **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).
135
Vincent Massol 39.1 136 == Apache ==
Jerome 16.1 137
Vincent Massol 39.1 138 Strategy:
Guillaume Fenollar 43.1 139
140 * 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
141 * 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 142 * Tell XWiki to not generate URLs with ##bin## in the path
Sergiu Dumitriu 4.1 143
Vincent Massol 39.1 144 Configuration steps:
Guillaume Fenollar 43.1 145
Vincent Massol 39.1 146 * Setup the following Apache configuration:(((
147 {{code language="apache"}}
Manuel Smeria 19.4 148 Alias /skins /usr/local/xwiki/skins
149 Alias /resources /usr/local/xwiki/resources
Jerome 19.1 150
Vincent Massol 34.1 151 RewriteEngine on
152
153 RewriteRule ^/+skins - [L]
154 RewriteCond %{REQUEST_URI} !\.gwtrpc$
155 RewriteRule ^/+resources($|/.*) - [L]
156
157 RewriteRule .* http://localhost:8080%{REQUEST_URI} [P,L]
158 ProxyPassReverse / http://localhost:8080/
159 {{/code}}
Vincent Massol 39.1 160 )))
Vincent Massol 41.1 161 * Edit ##web.xml## and add:(((
Vincent Massol 39.1 162 {{code language="xml"}}
163 <servlet-mapping>
164 <servlet-name>action</servlet-name>
165 <url-pattern>/*</url-pattern>
166 </servlet-mapping>
167 {{/code}}
168 )))
Vincent Massol 41.2 169 * Edit ##web.xml## and replace the existing mapping:(((
Vincent Massol 39.1 170 {{code language="xml"}}
171 <servlet-mapping>
172 <servlet-name>gwtrpc</servlet-name>
173 <url-pattern>*.gwtrpc</url-pattern>
174 </servlet-mapping>
175 {{/code}}
Vincent Massol 34.1 176
Vincent Massol 41.2 177 by
Vincent Massol 34.1 178
Vincent Massol 39.1 179 {{code language="xml"}}
180 <servlet-mapping>
181 <servlet-name>gwtrpc</servlet-name>
182 <url-pattern>/resources/*</url-pattern>
183 </servlet-mapping>
184 {{/code}}
185 )))
186 * Add the following in ##xwiki.cfg## (empty value after the equal sign):(((
Jerome 14.1 187 {{code language="none"}}
Vincent Massol 39.1 188 xwiki.defaultservletpath=
Jerome 14.1 189 {{/code}}
Guillaume Fenollar 43.1 190 )))
Vincent Massol 39.1 191 * {{info}}Only for XWiki 5.2+{{/info}} Add the following in ##xwiki.properties## (empty value after the equal sign):(((
192 {{code language="none"}}
Guillaume Fenollar 43.1 193 url.standard.getEntityPathPrefix=
Vincent Massol 39.1 194 {{/code}}
195 )))
PaulHarris 12.1 196
Vincent Massol 39.1 197 {{warning}}
198 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:
199 * Remove the 3 ##xwiki-platform-url-*## JARs you have in your ##WEB-INF/lib## directory and instead add the following:
200 ** 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
201 ** 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
202 ** 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
203 * Edit your ##xwiki.properties## file and add (no value after the equal sign):(((
Dmitry Bakbardin 23.1 204 {{code}}
Vincent Massol 39.1 205 url.standard.entityPathPrefix=
Dmitry Bakbardin 23.1 206 {{/code}}
Vincent Massol 39.1 207 )))
208 {{/warning}}
Dmitry Bakbardin 23.1 209
Dmitry Bakbardin 24.1 210
Dmitry Bakbardin 20.1 211 = III. Struts action name =
Jerome 14.1 212
Vincent Massol 50.2 213 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.
Manuel Smeria 19.4 214
Vincent Massol 50.2 215 It's possible to configure XWiki to allow omitting this action when you wish to imply ##view## and also to have XWiki generate URLs without the ##view## action. This is achieved by editing ##xwiki.cfg## and setting {{code language="none"}}xwiki.showviewaction=0{{/code}}.
Manuel Smeria 19.4 216
Vincent Massol 50.2 217 {{info}}
218 Before XWiki 7.2 and Nested Pages, XWiki was supporting dropping ##view## in the URL without changing the configuration. However this was broken in 7.2 in order to support Nested Pages.
219 {{/info}}
220
Dmitry Bakbardin 20.1 221 = IV. Error Page =
Manuel Smeria 19.4 222
Jerome 14.1 223 At the ##WEB-INF/web.xml##, the ##location## of the 404 error code needs to be changed accordingly. For example:
224
225 {{code language="xml"}}
226 <error-page>
mingfai 9.1 227 <error-code>404</error-code>
228 <!--<location>/xwiki/bin/view/Main/DocumentDoesNotExist</location>-->
229 <location>/bin/Main/DocumentDoesNotExist</location>
230 </error-page>
Jerome 14.1 231 {{/code}}
mingfai 9.1 232
Dmitry Bakbardin 20.1 233 = V. Conclusion =
Sergiu Dumitriu 4.1 234
235 After performing all these changes, you should be able to access documents with URLs like:
Jerome 14.1 236
Sergiu Dumitriu 4.1 237 * server.com/Space/Document
238 * server.com/Space/ (pointing to Space.WebHome)
239 * server.com/Document (pointing to Main.Document)
240 * server.com/ will show Main.WebHome, without any redirect.
241
Vincent Massol 5.1 242 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.
Valdis Vitolins 54.1 243
244
245 = Other solutions =
246
247 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 ;)
248
Ecaterina Moraru (Valica) 60.8 249 * [[Lighttpd + Jetty>>platform:Main.ShortURLsLighttpdJetty]]
Valdis Vitolins 58.1 250 * [[Apache + Tomcat>>http://odo.lv/Recipes/XWikiShortURLs?language=en]]

Get Connected