Wiki source code of Short XWiki URLs

Version 19.4 by Manuel Smeria on 2012/12/12

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 = 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 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.
16
17 = Servlet mapping name =
18
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.
20
21 == Original Instructions ==
22
23 In Jetty, the container shipped with the XWiki installer, you will have to write something like:
24
25 {{code language="xml"}}
26 <servlet>
27 <servlet-name>defaultSkins</servlet-name>
28 <servlet-class>org.mortbay.jetty.servlet.DefaultServlet</servlet-class>
29 <load-on-startup>1</load-on-startup>
30 </servlet>
31 <servlet-mapping>
32 <servlet-name>defaultSkins</servlet-name>
33 <url-pattern>/skins/*</url-pattern>
34 </servlet-mapping>
35 <servlet-mapping>
36 <servlet-name>defaultSkins</servlet-name>
37 <url-pattern>/resources/*</url-pattern>
38 </servlet-mapping>
39 {{/code}}
40
41 {{warning}}
42 If you are using Jetty 1.7 or higher, the correct servlet-class is ##org.eclipse.jetty.servlet.DefaultServlet##.
43 {{/warning}}
44
45 In Tomcat, the default servlet does not accept a parameter for changing the resource base, so you will need to write another default servlet.
46
47 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:
48
49 {{code}}
50 Alias /skins /usr/local/xwiki/skins
51 Alias /resources /usr/local/xwiki/resources
52 ProxyPass /skins/ !
53 ProxyPass /resources/ !
54 {{/code}}
55
56 The second thing to do is to copy the mapping for the Struts servlet to also be activated for /, like:
57
58 {{code language="xml"}}
59 <servlet-mapping>
60 <servlet-name>action</servlet-name>
61 <url-pattern>/*</url-pattern>
62 </servlet-mapping>
63 {{/code}}
64
65 Be sure to leave the other mappings in place, so that ##/bin/## works, too.
66
67 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}}.
68
69 == Specific Lighttpd + Jetty Instructions ==
70
71 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:
72
73 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:
74
75 * /resources/* ~-~-> static content
76 * /skins/* ~-~-> static content
77 * *.gwtrpc ~-~-> a servlet
78 * everything else ~-~-> other servlets
79
80 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.
81
82 So, we have to cheat a bit, and offload the static content to the webserver, which does have a powerful route-map configuration.
83
84 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)):
85
86 {{code language="none"}}
87 $HTTP["host"] =~ "^www\.domain\.com$" {
88 # ensure all requests for .gwtrpc files go through to java server
89 # we can put this rule first as a higher priority, which java couldn't do
90 $HTTP["url"] =~ "\.gwtrpc$" {
91 proxy.server = ( "" => (( "host" => "127.0.0.1", "port" => 8080 )))
92 }
93 # otherwise, we can handle the static resources
94 else $HTTP["url"] =~ "^/resources/" {
95 alias.url += ( "/resources" => "/usr/share/jetty/webapps/root/resources" )
96 }
97 # otherwise, we can handle the static resources
98 else $HTTP["url"] =~ "^/skins/" {
99 alias.url += ( "/skins" => "/usr/share/jetty/webapps/root/skins" )
100 }
101 # and here is the primary server
102 else $HTTP["host"] =~ "^www\.domain\.com$" {
103 proxy.server = ( "" => (( "host" => "127.0.0.1", "port" => 8080 )))
104 }
105 }
106 # redirect anything.domain.com to www.domain.com
107 else $HTTP["host"] =~ "\.domain\.com$" {
108 url.redirect = ( "^/(.*)" => "http://www.domain.com/$1" )
109 server.name = "www.domain.com"
110 }
111 # redirect domain.com to www.domain.com
112 else $HTTP["host"] =~ "domain\.com$" {
113 url.redirect = ( "^/(.*)" => "http://www.domain.com/$1" )
114 server.name = "www.domain.com"
115 }
116 {{/code}}
117
118 So lighttpd will serve any static content unless it has .gwtrpc on the end of the URL.
119
120 Then in ##web.xml##, I changed the gwtrpc mapping to:
121
122 {{code language="xml"}}
123 <servlet-mapping>
124 <servlet-name>gwtrpc</servlet-name>
125
126 <url-pattern>/resources/*</url-pattern>
127 <url-pattern>/skins/*</url-pattern>
128 </servlet-mapping>
129 {{/code}}
130
131 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.
132
133 Now, as described above, add a rule to catch everything else and redirect it to your xwiki servlet:
134
135 {{code language="xml"}}
136 <servlet-mapping>
137 <servlet-name>action</servlet-name>
138 <url-pattern>/*</url-pattern>
139 </servlet-mapping>
140 {{/code}}
141
142 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}}.
143
144 == Alternative: Changing the mapping name ==
145
146 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:
147
148 {{code language="xml"}}
149 <servlet-mapping>
150 <servlet-name>action</servlet-name>
151 <url-pattern>/wiki/*</url-pattern>
152 </servlet-mapping>
153 {{/code}}
154
155 This means that the wiki now accepts requests through this mapping.
156
157 {{info}}
158 This specific mapping (##/wiki##) should already be there, but you need to add a new one for other custom mappings.
159 {{/info}}
160
161 {{warning}}
162 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.
163 {{/warning}}
164
165 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:
166
167 {{code language="xml"}}
168 <servlet>
169 <servlet-name>redirectHomeServlet</servlet-name>
170 <servlet-class>com.xpn.xwiki.web.HomePageRedirectServlet</servlet-class>
171 <!-- Uncomment and edit this if you want to redirect to a different home page, or if you have different mappings.
172 Note: the URL should not start with /, because it allows the context name to be changed. If it starts with /,
173 then it should be an absolute URL, including the application context path. -->
174 <init-param>
175 <description>The address to redirect to when the client hits the root of the application.</description>
176 <param-name>homePage</param-name>
177 <param-value>wiki/</param-value>
178 </init-param>
179 </servlet>
180 {{/code}}
181
182 Also change the default mapping used by the URL factory, by adding this in ##xwiki.cfg##: {{code language="none"}}xwiki.defaultservletpath=wiki/{{/code}}.
183
184 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:
185
186 {{code language="none"}}
187 RedirectMatch ^/$ /xwiki/wiki/
188 RedirectMatch ^/xwiki/$ /xwiki/wiki/
189 {{/code}}
190
191 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.
192
193 = 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 = 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 = 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