Wiki source code of ShortURLs

Version 14.1 by Jerome on 2011/12/29

Show last authors
1 {{velocity filter="none"}}
2 {{html clean="false" wiki="true"}}
3 #startfloatingbox()
4 **Content**
5
6 {{toc start="" depth="" numbered=""/}}
7 #endfloatingbox()
8 {{/html}}
9 {{/velocity}}
10
11 = Short XWiki URLs =
12
13 This tutorial shows how to tune your XWiki platform by replacing the default URL scheme with a shorter scheme.
14
15 {{info}}
16 A short URL is an URL without the ##xwiki/bin/view## parts.
17 {{/info}}
18
19 == Application name ==
20
21 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##).
22
23 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.
24
25 {{warning}}
26 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:
27
28 {{code language="xml"}}<display-name></display-name>{{/code}}
29
30 {{/warning}}
31
32 == Struts action name ==
33
34 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.
35
36 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:
37
38 {{code language="none"}}
39 xwiki.showviewaction=0
40 {{/code}}
41
42 == Servlet mapping name ==
43
44 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.
45
46 === Original Instructions ===
47
48 In Jetty, the container shipped with the XWiki installer, you will have to write something like:
49
50 {{code language="xml"}}
51 <servlet>
52 <servlet-name>defaultSkins</servlet-name>
53 <servlet-class>org.mortbay.jetty.servlet.DefaultServlet</servlet-class>
54 <init-param>
55 <param-name>relativeResourceBase</param-name>
56 <param-value>skins</param-value>
57 </init-param>
58 <load-on-startup>1</load-on-startup>
59 </servlet>
60 <servlet-mapping>
61 <servlet-name>defaultSkins</servlet-name>
62 <url-pattern>/skins/*</url-pattern>
63 </servlet-mapping>
64 {{/code}}
65
66 In Tomcat, the default servlet does not accept a parameter for changing the resource base, so you will need to write another default servlet.
67
68 The second thing to do, is to copy the mapping for the Struts servlet to also be activated for /, like:
69
70 {{code language="xml"}}
71 <servlet-mapping>
72 <servlet-name>action</servlet-name>
73 <url-pattern>/*</url-pattern>
74 </servlet-mapping>
75 {{/code}}
76
77 Be sure to leave the other mappings in place, so that /bin/ works, too.
78
79 And the last thing that must be changed is the default mapping used by the URL factory, by adding in ##xwiki.cfg##:
80
81 {{code language="none"}}
82 xwiki.defaultservletpath=
83 {{/code}}
84
85 {{warning}}
86 Before 1.7, the setting name was ##xwiki.defaultactionpath##.
87 {{/warning}}
88
89 === Specific Lighttpd + Jetty Instructions ===
90
91 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.
92
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:
94
95 * /resources/* ~-~-> static content
96 * /skins/* ~-~-> static content
97 * *.gwtrpc ~-~-> a servlet
98 * everything else ~-~-> other servlets
99
100 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.
101
102 So, we have to cheat a bit, and offload the static content to the webserver, which DOES have a powerful route-map configuration.
103
104 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):
105
106 {{code language="none"}}
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 }
136 {{/code}}
137
138 So lighttpd will serve any static content UNLESS it has .gwtrpc on the end of the URL.
139
140 Then in xeb.xml, I change the gwtrpc mapping to:
141
142 {{code language="xml"}}
143 <servlet-mapping>
144 <servlet-name>gwtrpc</servlet-name>
145
146 <url-pattern>/resources/*</url-pattern>
147 <url-pattern>/skins/*</url-pattern>
148 </servlet-mapping>
149 {{/code}}
150
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 its ok. Note that only resources is required, but I did both anyway.
152
153 Now, as described above, add a rule to catch everything else and redirect it to our xwiki servlet.
154
155 {{code language="xml"}}
156 <servlet-mapping>
157 <servlet-name>action</servlet-name>
158 <url-pattern>/*</url-pattern>
159 </servlet-mapping>
160 {{/code}}
161
162 And the last thing that must be changed is the default mapping used by the URL factory, by adding in ##xwiki.cfg##:
163
164 {{code language="none"}}
165 xwiki.defaultservletpath=
166 {{/code}}
167
168 === Alternative: Changing the mapping name ===
169
170 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:
171
172 {{code language="xml"}}
173 <servlet-mapping>
174 <servlet-name>action</servlet-name>
175 <url-pattern>/wiki/*</url-pattern>
176 </servlet-mapping>
177 {{/code}}
178
179 This means that the wiki now accepts requests through this mapping.
180
181 {{info}}
182 This specific mapping (##/wiki##) should already be there, but you need to add a new one for other custom mappings.
183 {{/info}}
184
185 {{warning}}
186 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.
187 {{/warning}}
188
189 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:
190
191 {{code language="xml"}}
192 <servlet>
193 <servlet-name>redirectHomeServlet</servlet-name>
194 <servlet-class>com.xpn.xwiki.web.HomePageRedirectServlet</servlet-class>
195 <!-- Uncomment and edit this if you want to redirect to a different home page, or if you have different mappings.
196 Note: the URL should not start with /, because it allows the context name to be changed. If it starts with /,
197 then it should be an absolute URL, including the application context path. -->
198 <init-param>
199 <description>The address to redirect to when the client hits the root of the application.</description>
200 <param-name>homePage</param-name>
201 <param-value>wiki/</param-value>
202 </init-param>
203 </servlet>
204 {{/code}}
205
206 Also change the default mapping used by the URL factory, by adding in xwiki.cfg:
207
208 {{code language="none"}}
209 xwiki.defaultservletpath=wiki/
210 {{/code}}
211
212 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:##
213
214 {{code language="none"}}
215 RedirectMatch ^/$ /xwiki/wiki/
216 RedirectMatch ^/xwiki/$ /xwiki/wiki/
217 {{/code}}
218
219 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.
220
221 == Error Page ==
222
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>
227 <error-code>404</error-code>
228 <!--<location>/xwiki/bin/view/Main/DocumentDoesNotExist</location>-->
229 <location>/bin/Main/DocumentDoesNotExist</location>
230 </error-page>
231 {{/code}}
232
233 == Conclusion ==
234
235 After performing all these changes, you should be able to access documents with URLs like:
236
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
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.

Get Connected