Wiki source code of Short XWiki URLs

Version 46.1 by Vincent Massol on 2015/03/13

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##.
16
17 Achieving this depends on the container, as there's no standard regarding the ROOT application (Refer to your container's documentation for more details).
18
19 Some examples:
20 * 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##).
21 * 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:
22 ** 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.
23 ** Rename the existing ##webapps/xwiki## directory into ##webapps/root##.
24 ** 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.
25
26 In addition, starting with XWiki 6.2.7/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:
27
28 {{code language="none"}}
29 xwiki.webapppath=
30 {{/code}}
31
32 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.
33
34 = II. Servlet mapping name =
35
36 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).
37
38 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):
39
40 * ##/resources/*## and ##/skins/*##: Statically served resources. These need to be served directly as static resources.
41 * ##/rest/*##: REST resources, served by the XWiki REST Servlet
42 * ##/xmlrpc/*##: XML-RPC resources, served by the XWiki XMLRPC Servlet
43 * ##/resources/~*~*/*.gwtrpc##: GWT-RPC calls, served by the XWiki GWT Servlet
44 * ##/webdav/*##: WebDav calls, served by the XWiki WebDAV Servlet
45 * ##/XWikiService##: Another XWiki GWT Servlet
46 * ##/redirect##: The XWiki Redirect Servlet used to redirect to the home page when no page is specified in the URL
47
48 There are various alternate to achieve this:
49
50 {{toc scope="local"/}}
51
52 {{info}}
53 You might be tempted to configure just your XWiki's ##web.xml## file [[but this won't work>>ShortURLsInvalid]].
54 {{/info}}
55
56 == UrlRewriteFilter ==
57
58 {{info}}
59 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]].
60 {{/info}}
61
62 "UrlRewriteFilter" is a [[framework offering a Servlet Filter>>http://www.tuckey.org/urlrewrite/]] allowing to rewrite URLs.
63
64 Install steps:
65
66 1. [[Download the JAR>>http://www.tuckey.org/urlrewrite/]] and put it in ##WEB-INF/lib##
67 1. Edit your ##WEB-INF/web.xml## and add the ##<filter>## and ##filter-mapping## definitions [[as documented>>http://www.tuckey.org/urlrewrite/]]
68 1. Drop the following content in a file at ##WEB-INF/urlrewrite.xml##:(((
69 {{code language="xml"}}
70 <?xml version="1.0" encoding="utf-8"?>
71 <!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 4.0//EN"
72 "http://www.tuckey.org/res/dtds/urlrewrite4.0.dtd">
73 <urlrewrite decode-using="null">
74
75 <rule>
76 <note>
77 Ensure that URLs ending with .gwtrpc are not modified.
78 </note>
79 <from>^/(.*)\.gwtrpc$</from>
80 <to type="forward" last="true">-</to>
81 </rule>
82
83 <rule>
84 <note>
85 En sure that URLs that must not be served by the Struts Servlet are not modified.
86 </note>
87 <from>^/(bin|resources|skins|rest|webdav|xmlrpc|wiki)/(.*)$</from>
88 <to type="forward" last="true">-</to>
89 </rule>
90
91 <rule>
92 <note>
93 For all other URLs we prepend the "/bin/" prefix so that they get routed to the XWiki Action Servlet.
94 </note>
95 <from>^/(.*)$</from>
96 <to type="forward">/bin/$1</to>
97 </rule>
98
99 <outbound-rule>
100 <note>
101 Rewrite outbound URLs to remove the "/bin" part.
102 </note>
103 <from>/bin/(.*)/(.*)$</from>
104 <to>/$1/$2</to>
105 </outbound-rule>
106
107 </urlrewrite>
108 {{/code}}
109 )))
110
111 **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).
112
113 == Apache ==
114
115 Strategy:
116
117 * 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
118 * Configure ##web.xml## so that ##/*## URLs go through the Struts Servlet and so that ##/resources/~*~*/*.gwtrpc## URLs go through the GWT Servlet
119 * Tell XWiki to not generate URLs with ##bin## in the path
120
121 Configuration steps:
122
123 * Setup the following Apache configuration:(((
124 {{code language="apache"}}
125 Alias /skins /usr/local/xwiki/skins
126 Alias /resources /usr/local/xwiki/resources
127
128 RewriteEngine on
129
130 RewriteRule ^/+skins - [L]
131 RewriteCond %{REQUEST_URI} !\.gwtrpc$
132 RewriteRule ^/+resources($|/.*) - [L]
133
134 RewriteRule .* http://localhost:8080%{REQUEST_URI} [P,L]
135 ProxyPassReverse / http://localhost:8080/
136 {{/code}}
137 )))
138 * Edit ##web.xml## and add:(((
139 {{code language="xml"}}
140 <servlet-mapping>
141 <servlet-name>action</servlet-name>
142 <url-pattern>/*</url-pattern>
143 </servlet-mapping>
144 {{/code}}
145 )))
146 * Edit ##web.xml## and replace the existing mapping:(((
147 {{code language="xml"}}
148 <servlet-mapping>
149 <servlet-name>gwtrpc</servlet-name>
150 <url-pattern>*.gwtrpc</url-pattern>
151 </servlet-mapping>
152 {{/code}}
153
154 by
155
156 {{code language="xml"}}
157 <servlet-mapping>
158 <servlet-name>gwtrpc</servlet-name>
159 <url-pattern>/resources/*</url-pattern>
160 </servlet-mapping>
161 {{/code}}
162 )))
163 * Add the following in ##xwiki.cfg## (empty value after the equal sign):(((
164 {{code language="none"}}
165 xwiki.defaultservletpath=
166 {{/code}}
167 )))
168 * {{info}}Only for XWiki 5.2+{{/info}} Add the following in ##xwiki.properties## (empty value after the equal sign):(((
169 {{code language="none"}}
170 url.standard.getEntityPathPrefix=
171 {{/code}}
172 )))
173
174 {{warning}}
175 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:
176 * Remove the 3 ##xwiki-platform-url-*## JARs you have in your ##WEB-INF/lib## directory and instead add the following:
177 ** 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
178 ** 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
179 ** 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
180 * Edit your ##xwiki.properties## file and add (no value after the equal sign):(((
181 {{code}}
182 url.standard.entityPathPrefix=
183 {{/code}}
184 )))
185 {{/warning}}
186
187 == Others ==
188
189 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 ;)
190
191 * [[Lighttpd + Jetty>>ShortURLsLighttpdJetty]]
192
193 = III. Struts action name =
194
195 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.
196
197 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}}.
198
199 = IV. Error Page =
200
201 At the ##WEB-INF/web.xml##, the ##location## of the 404 error code needs to be changed accordingly. For example:
202
203 {{code language="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 = V. Conclusion =
212
213 After performing all these changes, you should be able to access documents with URLs like:
214
215 * server.com/Space/Document
216 * server.com/Space/ (pointing to Space.WebHome)
217 * server.com/Document (pointing to Main.Document)
218 * server.com/ will show Main.WebHome, without any redirect.
219
220 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