Wiki source code of Short XWiki URLs

Version 20.1 by Dmitry Bakbardin on 2012/12/29

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
Manuel Smeria 19.4 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 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##). In Jetty, the default name is ##root##. Refer to your container's documentation for more details.
Vincent Massol 10.2 16
Dmitry Bakbardin 20.1 17 = II. Servlet mapping name =
maalej 1.1 18
Manuel Smeria 19.4 19 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.
maalej 1.1 20
Dmitry Bakbardin 20.1 21 == 2.1. Original Instructions ==
maalej 1.1 22
Dmitry Bakbardin 20.1 23 === 2.1.1. Container setup ===
24
PaulHarris 12.1 25 In Jetty, the container shipped with the XWiki installer, you will have to write something like:
26
Jerome 14.1 27 {{code language="xml"}}
28 <servlet>
Sergiu Dumitriu 4.1 29 <servlet-name>defaultSkins</servlet-name>
PaulHarris 12.1 30 <servlet-class>org.mortbay.jetty.servlet.DefaultServlet</servlet-class>
Sergiu Dumitriu 4.1 31 <load-on-startup>1</load-on-startup>
32 </servlet>
33 <servlet-mapping>
34 <servlet-name>defaultSkins</servlet-name>
35 <url-pattern>/skins/*</url-pattern>
36 </servlet-mapping>
Jerome 18.1 37 <servlet-mapping>
38 <servlet-name>defaultSkins</servlet-name>
39 <url-pattern>/resources/*</url-pattern>
40 </servlet-mapping>
Jerome 14.1 41 {{/code}}
Sergiu Dumitriu 4.1 42
Jerome 16.1 43 {{warning}}
Manuel Smeria 19.4 44 If you are using Jetty 1.7 or higher, the correct servlet-class is ##org.eclipse.jetty.servlet.DefaultServlet##.
Jerome 16.1 45 {{/warning}}
46
Sergiu Dumitriu 7.1 47 In Tomcat, the default servlet does not accept a parameter for changing the resource base, so you will need to write another default servlet.
Sergiu Dumitriu 4.1 48
Dmitry Bakbardin 20.1 49 === 2.1.2. Container setup alternative: front-end by-pass ===
50
Manuel Smeria 19.4 51 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:
Jerome 19.1 52
53 {{code}}
Manuel Smeria 19.4 54 Alias /skins /usr/local/xwiki/skins
55 Alias /resources /usr/local/xwiki/resources
56 ProxyPass /skins/ !
57 ProxyPass /resources/ !
Jerome 19.1 58 {{/code}}
59
Dmitry Bakbardin 20.1 60 If you use Nginx as a web-server, just add two more locations and set "root" to them:
61
62 {{code}}
63 location /skins/ {
64 root /var/lib/tomcat7/webapps/ROOT;
65 }
66
67 location /resources/ {
68 root /var/lib/tomcat7/webapps/ROOT;
69 }
70 {{/code}}
71
72 In the example above XWiki is set as ROOT applocation in Tomcat. Change path to your installation accordingly.
73
74 === 2.2. Struts servlet mapping ===
75
Manuel Smeria 19.4 76 The second thing to do is to copy the mapping for the Struts servlet to also be activated for /, like:
Jerome 14.1 77
78 {{code language="xml"}}
79 <servlet-mapping>
Sergiu Dumitriu 4.1 80 <servlet-name>action</servlet-name>
81 <url-pattern>/*</url-pattern>
82 </servlet-mapping>
Jerome 14.1 83 {{/code}}
84
Manuel Smeria 19.4 85 Be sure to leave the other mappings in place, so that ##/bin/## works, too.
Sergiu Dumitriu 4.1 86
Manuel Smeria 19.4 87 Tthe 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}}.
Sergiu Dumitriu 4.1 88
Dmitry Bakbardin 20.1 89 == 2.3. Specific Lighttpd + Jetty Instructions ==
Sergiu Dumitriu 11.1 90
Manuel Smeria 19.4 91 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:
PaulHarris 12.1 92
Jerome 14.1 93 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:
PaulHarris 12.1 94
Jerome 14.1 95 * /resources/* ~-~-> static content
96 * /skins/* ~-~-> static content
97 * *.gwtrpc ~-~-> a servlet
98 * everything else ~-~-> other servlets
99
Manuel Smeria 19.4 100 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.
PaulHarris 12.1 101
Manuel Smeria 19.4 102 So, we have to cheat a bit, and offload the static content to the webserver, which does have a powerful route-map configuration.
PaulHarris 12.1 103
Manuel Smeria 19.4 104 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)):
PaulHarris 12.1 105
Jerome 14.1 106 {{code language="none"}}
PaulHarris 12.1 107 $HTTP["host"] =~ "^www\.domain\.com$" {
108 # ensure all requests for .gwtrpc files go through to java server
109 # we can put this rule first as a higher priority, which java couldn't do
110 $HTTP["url"] =~ "\.gwtrpc$" {
111 proxy.server = ( "" => (( "host" => "127.0.0.1", "port" => 8080 )))
112 }
113 # otherwise, we can handle the static resources
114 else $HTTP["url"] =~ "^/resources/" {
115 alias.url += ( "/resources" => "/usr/share/jetty/webapps/root/resources" )
116 }
117 # otherwise, we can handle the static resources
118 else $HTTP["url"] =~ "^/skins/" {
119 alias.url += ( "/skins" => "/usr/share/jetty/webapps/root/skins" )
120 }
121 # and here is the primary server
122 else $HTTP["host"] =~ "^www\.domain\.com$" {
123 proxy.server = ( "" => (( "host" => "127.0.0.1", "port" => 8080 )))
124 }
125 }
126 # redirect anything.domain.com to www.domain.com
127 else $HTTP["host"] =~ "\.domain\.com$" {
128 url.redirect = ( "^/(.*)" => "http://www.domain.com/$1" )
129 server.name = "www.domain.com"
130 }
131 # redirect domain.com to www.domain.com
132 else $HTTP["host"] =~ "domain\.com$" {
133 url.redirect = ( "^/(.*)" => "http://www.domain.com/$1" )
134 server.name = "www.domain.com"
135 }
Jerome 14.1 136 {{/code}}
PaulHarris 12.1 137
Manuel Smeria 19.4 138 So lighttpd will serve any static content unless it has .gwtrpc on the end of the URL.
PaulHarris 12.1 139
Manuel Smeria 19.4 140 Then in ##web.xml##, I changed the gwtrpc mapping to:
Jerome 14.1 141
142 {{code language="xml"}}
143 <servlet-mapping>
PaulHarris 12.1 144 <servlet-name>gwtrpc</servlet-name>
145
146 <url-pattern>/resources/*</url-pattern>
147 <url-pattern>/skins/*</url-pattern>
148 </servlet-mapping>
Jerome 14.1 149 {{/code}}
PaulHarris 12.1 150
Manuel Smeria 19.4 151 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.
PaulHarris 12.1 152
Manuel Smeria 19.4 153 Now, as described above, add a rule to catch everything else and redirect it to your xwiki servlet:
Jerome 14.1 154
155 {{code language="xml"}}
156 <servlet-mapping>
PaulHarris 12.1 157 <servlet-name>action</servlet-name>
158 <url-pattern>/*</url-pattern>
159 </servlet-mapping>
Jerome 14.1 160 {{/code}}
PaulHarris 12.1 161
Manuel Smeria 19.4 162 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}}.
PaulHarris 12.1 163
Dmitry Bakbardin 20.1 164 == 2.4. Alternative: Changing the mapping name ==
PaulHarris 12.1 165
Manuel Smeria 19.4 166 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:
PaulHarris 12.1 167
Jerome 14.1 168 {{code language="xml"}}
169 <servlet-mapping>
Sergiu Dumitriu 11.3 170 <servlet-name>action</servlet-name>
171 <url-pattern>/wiki/*</url-pattern>
172 </servlet-mapping>
Jerome 14.1 173 {{/code}}
Sergiu Dumitriu 11.3 174
175 This means that the wiki now accepts requests through this mapping.
176
Jerome 14.1 177 {{info}}
178 This specific mapping (##/wiki##) should already be there, but you need to add a new one for other custom mappings.
179 {{/info}}
Sergiu Dumitriu 11.3 180
Jerome 14.1 181 {{warning}}
Manuel Smeria 19.4 182 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.
Jerome 14.1 183 {{/warning}}
Sergiu Dumitriu 11.4 184
Manuel Smeria 19.4 185 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:
Sergiu Dumitriu 11.3 186
Jerome 14.1 187 {{code language="xml"}}
188 <servlet>
Sergiu Dumitriu 11.3 189 <servlet-name>redirectHomeServlet</servlet-name>
190 <servlet-class>com.xpn.xwiki.web.HomePageRedirectServlet</servlet-class>
191 <!-- Uncomment and edit this if you want to redirect to a different home page, or if you have different mappings.
192 Note: the URL should not start with /, because it allows the context name to be changed. If it starts with /,
193 then it should be an absolute URL, including the application context path. -->
194 <init-param>
195 <description>The address to redirect to when the client hits the root of the application.</description>
196 <param-name>homePage</param-name>
197 <param-value>wiki/</param-value>
198 </init-param>
199 </servlet>
Jerome 14.1 200 {{/code}}
Sergiu Dumitriu 11.3 201
Manuel Smeria 19.4 202 Also change the default mapping used by the URL factory, by adding this in ##xwiki.cfg##: {{code language="none"}}xwiki.defaultservletpath=wiki/{{/code}}.
Sergiu Dumitriu 11.3 203
Manuel Smeria 19.4 204 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:
Sergiu Dumitriu 11.3 205
Jerome 14.1 206 {{code language="none"}}
207 RedirectMatch ^/$ /xwiki/wiki/
Sergiu Dumitriu 11.3 208 RedirectMatch ^/xwiki/$ /xwiki/wiki/
Jerome 14.1 209 {{/code}}
Sergiu Dumitriu 11.3 210
Jerome 14.1 211 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.
Sergiu Dumitriu 11.3 212
Dmitry Bakbardin 20.1 213 = III. Struts action name =
Jerome 14.1 214
Manuel Smeria 19.4 215 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.
216
217 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}}.
218
Dmitry Bakbardin 20.1 219 = IV. Error Page =
Manuel Smeria 19.4 220
Jerome 14.1 221 At the ##WEB-INF/web.xml##, the ##location## of the 404 error code needs to be changed accordingly. For example:
222
223 {{code language="xml"}}
224 <error-page>
mingfai 9.1 225 <error-code>404</error-code>
226 <!--<location>/xwiki/bin/view/Main/DocumentDoesNotExist</location>-->
227 <location>/bin/Main/DocumentDoesNotExist</location>
228 </error-page>
Jerome 14.1 229 {{/code}}
mingfai 9.1 230
Dmitry Bakbardin 20.1 231 = V. Conclusion =
Sergiu Dumitriu 4.1 232
233 After performing all these changes, you should be able to access documents with URLs like:
Jerome 14.1 234
Sergiu Dumitriu 4.1 235 * server.com/Space/Document
236 * server.com/Space/ (pointing to Space.WebHome)
237 * server.com/Document (pointing to Main.Document)
238 * server.com/ will show Main.WebHome, without any redirect.
239
Vincent Massol 5.1 240 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