Wiki source code of ShortURLs

Version 19.1 by Jerome on 2012/07/10

Hide last authors
Jerome 14.1 1 {{velocity filter="none"}}
Jerome 15.1 2 {{html wiki=true clean=false}}
Vincent Massol 6.1 3 #startfloatingbox()
Jerome 14.1 4 **Content**
5
Jerome 15.1 6 {{toc /}}
7
Vincent Massol 6.1 8 #endfloatingbox()
Jerome 14.1 9 {{/html}}
10 {{/velocity}}
Vincent Massol 6.1 11
Jerome 14.1 12 = Short XWiki URLs =
maalej 1.1 13
Vincent Massol 5.1 14 This tutorial shows how to tune your XWiki platform by replacing the default URL scheme with a shorter scheme.
maalej 3.1 15
Jerome 14.1 16 {{info}}
17 A short URL is an URL without the ##xwiki/bin/view## parts.
18 {{/info}}
maalej 2.1 19
Jerome 14.1 20 == Application name ==
maalej 1.1 21
Jerome 14.1 22 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 23
Jerome 14.1 24 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 again 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 25
Jerome 14.1 26 {{warning}}
27 Before XWiki Platform 1.3, the the ##display-name## parameter in ##web.xml## must also be changed to reflect the new application name. For example if you've deployed the XWiki webapp as ROOT then you'll need to have:
maalej 1.1 28
Jerome 14.1 29 {{code language="xml"}}<display-name></display-name>{{/code}}
maalej 1.1 30
Jerome 14.1 31 {{/warning}}
maalej 1.1 32
Jerome 14.1 33 == Struts action name ==
maalej 1.1 34
Jerome 14.1 35 The third part, /view/, identifies the struts action that should process a request. So this tells what do 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.
Sergiu Dumitriu 4.1 36
Jerome 14.1 37 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, in ##xwiki.cfg## write:
Sergiu Dumitriu 4.1 38
Jerome 14.1 39 {{code language="none"}}
40 xwiki.showviewaction=0
41 {{/code}}
Sergiu Dumitriu 4.1 42
Jerome 14.1 43 == Servlet mapping name ==
PaulHarris 12.1 44
Jerome 14.1 45 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.
PaulHarris 12.1 46
Jerome 14.1 47 === Original Instructions ===
48
PaulHarris 12.1 49 In Jetty, the container shipped with the XWiki installer, you will have to write something like:
50
Jerome 14.1 51 {{code language="xml"}}
52 <servlet>
Sergiu Dumitriu 4.1 53 <servlet-name>defaultSkins</servlet-name>
PaulHarris 12.1 54 <servlet-class>org.mortbay.jetty.servlet.DefaultServlet</servlet-class>
Sergiu Dumitriu 4.1 55 <load-on-startup>1</load-on-startup>
56 </servlet>
57 <servlet-mapping>
58 <servlet-name>defaultSkins</servlet-name>
59 <url-pattern>/skins/*</url-pattern>
60 </servlet-mapping>
Jerome 18.1 61 <servlet-mapping>
62 <servlet-name>defaultSkins</servlet-name>
63 <url-pattern>/resources/*</url-pattern>
64 </servlet-mapping>
Jerome 14.1 65 {{/code}}
Sergiu Dumitriu 4.1 66
Jerome 16.1 67 {{warning}}
68 If you are using jetty 1.7 or higher, the correct servlet-class is ##org.eclipse.jetty.servlet.DefaultServlet##
69 {{/warning}}
70
Sergiu Dumitriu 7.1 71 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 72
Jerome 19.1 73 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 and webapp deployed as a ROOT webapp and an AJP connection between httpd and the servlet container, the following configuration you allow to serve skin files and and static resources directly from httpd:
74
75 {{code}}
76 Alias /skins /usr/local/xwiki/skins
77 Alias /resources /usr/local/xwiki/resources
78 ProxyPass /skins/ !
79 ProxyPass /resources/ !
80 {{/code}}
81
Sergiu Dumitriu 4.1 82 The second thing to do, is to copy the mapping for the Struts servlet to also be activated for /, like:
Jerome 14.1 83
84 {{code language="xml"}}
85 <servlet-mapping>
Sergiu Dumitriu 4.1 86 <servlet-name>action</servlet-name>
87 <url-pattern>/*</url-pattern>
88 </servlet-mapping>
Jerome 14.1 89 {{/code}}
90
Sergiu Dumitriu 4.1 91 Be sure to leave the other mappings in place, so that /bin/ works, too.
92
Jerome 14.1 93 And the last thing that must be changed is the default mapping used by the URL factory, by adding in ##xwiki.cfg##:
Sergiu Dumitriu 4.1 94
Jerome 14.1 95 {{code language="none"}}
96 xwiki.defaultservletpath=
97 {{/code}}
Sergiu Dumitriu 11.1 98
Jerome 14.1 99 {{warning}}
100 Before 1.7, the setting name was ##xwiki.defaultactionpath##.
101 {{/warning}}
PaulHarris 12.1 102
Jerome 14.1 103 === Specific Lighttpd + Jetty Instructions ===
PaulHarris 12.1 104
Jerome 14.1 105 The original instructions MIGHT work for you, if you don't use the WYSIWYG editor. It did not work for me, this is what I had to do.
PaulHarris 12.1 106
Jerome 14.1 107 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 108
Jerome 14.1 109 * /resources/* ~-~-> static content
110 * /skins/* ~-~-> static content
111 * *.gwtrpc ~-~-> a servlet
112 * everything else ~-~-> other servlets
113
PaulHarris 12.1 114 The problem is that .gwtrpc files are "in" the /resources folder, and as the /resources/* mapping will always be a higher priority than *.gwtrpc due to the way structs works.
115
116 So, we have to cheat a bit, and offload the static content to the webserver, which DOES have a powerful route-map configuration.
117
Jerome 14.1 118 I use lighttpd, but I assume it can be done in other webservers. 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 119
Jerome 14.1 120 {{code language="none"}}
PaulHarris 12.1 121 $HTTP["host"] =~ "^www\.domain\.com$" {
122 # ensure all requests for .gwtrpc files go through to java server
123 # we can put this rule first as a higher priority, which java couldn't do
124 $HTTP["url"] =~ "\.gwtrpc$" {
125 proxy.server = ( "" => (( "host" => "127.0.0.1", "port" => 8080 )))
126 }
127 # otherwise, we can handle the static resources
128 else $HTTP["url"] =~ "^/resources/" {
129 alias.url += ( "/resources" => "/usr/share/jetty/webapps/root/resources" )
130 }
131 # otherwise, we can handle the static resources
132 else $HTTP["url"] =~ "^/skins/" {
133 alias.url += ( "/skins" => "/usr/share/jetty/webapps/root/skins" )
134 }
135 # and here is the primary server
136 else $HTTP["host"] =~ "^www\.domain\.com$" {
137 proxy.server = ( "" => (( "host" => "127.0.0.1", "port" => 8080 )))
138 }
139 }
140 # redirect anything.domain.com to www.domain.com
141 else $HTTP["host"] =~ "\.domain\.com$" {
142 url.redirect = ( "^/(.*)" => "http://www.domain.com/$1" )
143 server.name = "www.domain.com"
144 }
145 # redirect domain.com to www.domain.com
146 else $HTTP["host"] =~ "domain\.com$" {
147 url.redirect = ( "^/(.*)" => "http://www.domain.com/$1" )
148 server.name = "www.domain.com"
149 }
Jerome 14.1 150 {{/code}}
PaulHarris 12.1 151
152 So lighttpd will serve any static content UNLESS it has .gwtrpc on the end of the URL.
153
Jerome 17.1 154 Then in web.xml, I change the gwtrpc mapping to:
Jerome 14.1 155
156 {{code language="xml"}}
157 <servlet-mapping>
PaulHarris 12.1 158 <servlet-name>gwtrpc</servlet-name>
159
160 <url-pattern>/resources/*</url-pattern>
161 <url-pattern>/skins/*</url-pattern>
162 </servlet-mapping>
Jerome 14.1 163 {{/code}}
PaulHarris 12.1 164
Jerome 14.1 165 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 its ok. Note that only resources is required, but I did both anyway.
PaulHarris 12.1 166
167 Now, as described above, add a rule to catch everything else and redirect it to our xwiki servlet.
Jerome 14.1 168
169 {{code language="xml"}}
170 <servlet-mapping>
PaulHarris 12.1 171 <servlet-name>action</servlet-name>
172 <url-pattern>/*</url-pattern>
173 </servlet-mapping>
Jerome 14.1 174 {{/code}}
PaulHarris 12.1 175
Jerome 14.1 176 And the last thing that must be changed is the default mapping used by the URL factory, by adding in ##xwiki.cfg##:
PaulHarris 12.1 177
Jerome 14.1 178 {{code language="none"}}
179 xwiki.defaultservletpath=
180 {{/code}}
PaulHarris 12.1 181
Jerome 14.1 182 === Alternative: Changing the mapping name ===
PaulHarris 12.1 183
Jerome 14.1 184 If removing the ##/bin## part is not possible in your environment, you can still rename it to something less technical, and which would fit better in your site, like ##/wiki##. To do this, you must first add a mapping for the new path, as in:
Sergiu Dumitriu 11.3 185
Jerome 14.1 186 {{code language="xml"}}
187 <servlet-mapping>
Sergiu Dumitriu 11.3 188 <servlet-name>action</servlet-name>
189 <url-pattern>/wiki/*</url-pattern>
190 </servlet-mapping>
Jerome 14.1 191 {{/code}}
Sergiu Dumitriu 11.3 192
193 This means that the wiki now accepts requests through this mapping.
194
Jerome 14.1 195 {{info}}
196 This specific mapping (##/wiki##) should already be there, but you need to add a new one for other custom mappings.
197 {{/info}}
Sergiu Dumitriu 11.3 198
Jerome 14.1 199 {{warning}}
200 The ##/wiki## mapping is reserved for multiwiki 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.
201 {{/warning}}
Sergiu Dumitriu 11.4 202
Jerome 14.1 203 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 204
Jerome 14.1 205 {{code language="xml"}}
206 <servlet>
Sergiu Dumitriu 11.3 207 <servlet-name>redirectHomeServlet</servlet-name>
208 <servlet-class>com.xpn.xwiki.web.HomePageRedirectServlet</servlet-class>
209 <!-- Uncomment and edit this if you want to redirect to a different home page, or if you have different mappings.
210 Note: the URL should not start with /, because it allows the context name to be changed. If it starts with /,
211 then it should be an absolute URL, including the application context path. -->
212 <init-param>
213 <description>The address to redirect to when the client hits the root of the application.</description>
214 <param-name>homePage</param-name>
215 <param-value>wiki/</param-value>
216 </init-param>
217 </servlet>
Jerome 14.1 218 {{/code}}
Sergiu Dumitriu 11.3 219
220 Also change the default mapping used by the URL factory, by adding in xwiki.cfg:
221
Jerome 14.1 222 {{code language="none"}}
Sergiu Dumitriu 11.3 223 xwiki.defaultservletpath=wiki/
Jerome 14.1 224 {{/code}}
Sergiu Dumitriu 11.3 225
Jerome 14.1 226 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 the settings:##
Sergiu Dumitriu 11.3 227
Jerome 14.1 228 {{code language="none"}}
229 RedirectMatch ^/$ /xwiki/wiki/
Sergiu Dumitriu 11.3 230 RedirectMatch ^/xwiki/$ /xwiki/wiki/
Jerome 14.1 231 {{/code}}
Sergiu Dumitriu 11.3 232
Jerome 14.1 233 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 234
Jerome 14.1 235 == Error Page ==
236
237 At the ##WEB-INF/web.xml##, the ##location## of the 404 error code needs to be changed accordingly. For example:
238
239 {{code language="xml"}}
240 <error-page>
mingfai 9.1 241 <error-code>404</error-code>
242 <!--<location>/xwiki/bin/view/Main/DocumentDoesNotExist</location>-->
243 <location>/bin/Main/DocumentDoesNotExist</location>
244 </error-page>
Jerome 14.1 245 {{/code}}
mingfai 9.1 246
Jerome 14.1 247 == Conclusion ==
Sergiu Dumitriu 4.1 248
249 After performing all these changes, you should be able to access documents with URLs like:
Jerome 14.1 250
Sergiu Dumitriu 4.1 251 * server.com/Space/Document
252 * server.com/Space/ (pointing to Space.WebHome)
253 * server.com/Document (pointing to Main.Document)
254 * server.com/ will show Main.WebHome, without any redirect.
255
Vincent Massol 5.1 256 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