Wiki source code of Short XWiki URLs
Version 36.1 by Vincent Massol on 2013/09/19
Show last authors
author | version | line-number | content |
---|---|---|---|
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##. Achieving this depends on the container, as there's no standard regarding the ROOT application. For example: | ||
16 | * 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##). | ||
17 | * 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: | ||
18 | ** 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. | ||
19 | ** Rename the existing ##webapps/xwiki## directory into ##webapps/root##. | ||
20 | ** 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. | ||
21 | |||
22 | Refer to your container's documentation for more details. | ||
23 | |||
24 | = II. Servlet mapping name = | ||
25 | |||
26 | 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). | ||
27 | |||
28 | 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): | ||
29 | * ##/resources/*## and ##/skins/*##: Statically served resources. These need to be served directly as static resources. | ||
30 | * ##/rest/*##: REST resources, served by the XWiki REST Servlet | ||
31 | * ##/xmlrpc/*##: XML-RPC resources, served by the XWiki XMLRPC Servlet | ||
32 | * ##/resources/*~*/*.gwtrpc##: GWT-RPC calls, served by the XWiki GWT Servlet | ||
33 | * ##/webdav/*##: WebDav calls, served by the XWiki WebDAV Servlet | ||
34 | * ##/XWikiService##: Another XWiki GWT Servlet | ||
35 | * ##/redirect##: The XWiki Redirect Servlet used to redirect to the home page when no page is specified in the URL | ||
36 | |||
37 | There are various alternate ways to achieve this as detailed below. | ||
38 | |||
39 | XWiki also generates URL and you can tell it to generate URLs without the ##bin/## part by adding this piece of code in ##xwiki.cfg##: {{code language="none"}}xwiki.defaultservletpath={{/code}} (Note that this is not required with the UrlRewriteFilter solution described below since it rewrites outbound URLs too). | ||
40 | |||
41 | == UrlRewriteFilter == | ||
42 | |||
43 | {{info}}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]].{{/info}} | ||
44 | |||
45 | "UrlRewriteFilter" is a [[framework offering a Servlet Filter>>http://www.tuckey.org/urlrewrite/]] allowing to rewrite URLs. | ||
46 | |||
47 | Install steps: | ||
48 | 1. [[Download the JAR>>http://www.tuckey.org/urlrewrite/]] and put it in ##WEB-INF/lib## | ||
49 | 1. Edit your ##WEB-INF/web.xml## and add the ##<filter>## and ##filter-mapping## definitions [[as documented>>http://www.tuckey.org/urlrewrite/]] | ||
50 | 1. Drop the following content in a file at ##WEB-INF/urlrewrite.xml##:((( | ||
51 | {{code language="xml"}} | ||
52 | <?xml version="1.0" encoding="utf-8"?> | ||
53 | <!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 4.0//EN" | ||
54 | "http://www.tuckey.org/res/dtds/urlrewrite4.0.dtd"> | ||
55 | <urlrewrite> | ||
56 | |||
57 | <rule> | ||
58 | <note> | ||
59 | Ensure that URLs ending with .gwtrpc are not modified. | ||
60 | </note> | ||
61 | <from>^/(.*)\.gwtrpc$</from> | ||
62 | <to type="forward" last="true">-</to> | ||
63 | </rule> | ||
64 | |||
65 | <rule> | ||
66 | <note> | ||
67 | En sure that URLs that must not be served by the Struts Servlet are not modified. | ||
68 | </note> | ||
69 | <from>^/(bin|resources|skins|rest|webdav|xmlrpc)/(.*)$</from> | ||
70 | <to type="forward" last="true">-</to> | ||
71 | </rule> | ||
72 | |||
73 | <rule> | ||
74 | <note> | ||
75 | For all other URLs we prepend the "/bin/" prefix so that they get routed to the XWiki Action Servlet. | ||
76 | </note> | ||
77 | <from>^/(.*)$</from> | ||
78 | <to type="forward">/bin/$1</to> | ||
79 | </rule> | ||
80 | |||
81 | <outbound-rule> | ||
82 | <note> | ||
83 | Rewrite outbound URLs to remove the "/bin" part. | ||
84 | </note> | ||
85 | <from>/bin/(.*)/(.*)$</from> | ||
86 | <to>/$1/$2</to> | ||
87 | </outbound-rule> | ||
88 | |||
89 | </urlrewrite> | ||
90 | {{/code}} | ||
91 | ))) | ||
92 | |||
93 | == Configuring web.xml == | ||
94 | |||
95 | You could be tempted to configure the XWiki's ##web.xml## file as follows below but **note that the WYSIWYG editor won't work with this solution**. The problem is that the ##/*## mapping will override the ##/*.gwtrpc## mapping. This is caused by the Servlet spec which says: | ||
96 | |||
97 | > The URL path mapping rules below are used in order. The first successful match is used with no further matches attempted: | ||
98 | > 1. The container will try to find an exact match of the path of the request to the path of the servlet. A successful match selects the servlet. | ||
99 | > 2. The container will recursively try to match the longest path-prefix. This is done by stepping down the path tree a directory at a time, using the '/' character as a path separator. The longest match determines the servlet selected. | ||
100 | > 3. If the last segment in the URL path contains an extension (e.g. .jsp), the servlet container will try to match a servlet that handles requests for the extension. An extension is defined as the part of the last segment after the last '.' character. | ||
101 | > 4. If neither of the previous three rules result in a servlet match, the container will attempt to serve content appropriate for the resource requested. If a "default" servlet is defined for the application, it will be used. | ||
102 | |||
103 | In any case if you don't use the WYSIWYG editor this could be a good solution so here it is just in case: | ||
104 | * Copy the mapping for the Struts servlet to also be activated for ##/*##:((( | ||
105 | {{code language="xml"}} | ||
106 | <servlet-mapping> | ||
107 | <servlet-name>action</servlet-name> | ||
108 | <url-pattern>/*</url-pattern> | ||
109 | </servlet-mapping> | ||
110 | {{/code}} | ||
111 | |||
112 | Be sure to leave the other mappings in place, so that ##/bin/## works, too. | ||
113 | ))) | ||
114 | * Configure your container to statically serve ##/resources/*## and ##/skins/*## resources. By default we don't have any mapping for those in ##web.xml## which means they are statically served (by the Default Servlet). Configuring the Default Servlet is container-dependent unfortunately. | ||
115 | ** In Jetty, the container shipped with the XWiki installer, you will have to write: | ||
116 | *** For Jetty <= 6.x:((( | ||
117 | {{code language="xml"}} | ||
118 | <servlet> | ||
119 | <servlet-name>defaultSkins</servlet-name> | ||
120 | <servlet-class>org.mortbay.jetty.servlet.DefaultServlet</servlet-class> | ||
121 | <load-on-startup>1</load-on-startup> | ||
122 | </servlet> | ||
123 | <servlet-mapping> | ||
124 | <servlet-name>defaultSkins</servlet-name> | ||
125 | <url-pattern>/skins/*</url-pattern> | ||
126 | </servlet-mapping> | ||
127 | <servlet-mapping> | ||
128 | <servlet-name>defaultSkins</servlet-name> | ||
129 | <url-pattern>/resources/*</url-pattern> | ||
130 | </servlet-mapping> | ||
131 | {{/code}} | ||
132 | ))) | ||
133 | *** For Jetty >= 7.x:((( | ||
134 | {{code language="xml"}} | ||
135 | <servlet> | ||
136 | <servlet-name>defaultSkins</servlet-name> | ||
137 | <servlet-class>org.eclipse.jetty.servlet.DefaultServlet</servlet-class> | ||
138 | <load-on-startup>1</load-on-startup> | ||
139 | </servlet> | ||
140 | <servlet-mapping> | ||
141 | <servlet-name>defaultSkins</servlet-name> | ||
142 | <url-pattern>/skins/*</url-pattern> | ||
143 | </servlet-mapping> | ||
144 | <servlet-mapping> | ||
145 | <servlet-name>defaultSkins</servlet-name> | ||
146 | <url-pattern>/resources/*</url-pattern> | ||
147 | </servlet-mapping> | ||
148 | {{/code}} | ||
149 | ))) | ||
150 | ** In Tomcat, the default Servlet does not accept a parameter for changing the resource base, so you would need to write another default servlet. | ||
151 | |||
152 | == web.xml + Apache == | ||
153 | |||
154 | Do as above but instead of mapping the Default Servlet, bypass 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: | ||
155 | |||
156 | {{code}} | ||
157 | Alias /skins /usr/local/xwiki/skins | ||
158 | Alias /resources /usr/local/xwiki/resources | ||
159 | ProxyPass /skins/ ! | ||
160 | ProxyPass /resources/ ! | ||
161 | {{/code}} | ||
162 | |||
163 | Note that this solution will not work for the WYSIWYG editor since it needs that the ##.gwtrpc## URLs are directed to the XWiki Servlet. The following Apache configuration using URL rewriting achieves this: | ||
164 | |||
165 | {{code}} | ||
166 | Alias /skins /usr/local/xwiki/skins | ||
167 | Alias /resources /usr/local/xwiki/resources | ||
168 | |||
169 | RewriteEngine on | ||
170 | |||
171 | RewriteRule ^/+skins - [L] | ||
172 | RewriteCond %{REQUEST_URI} !\.gwtrpc$ | ||
173 | RewriteRule ^/+resources($|/.*) - [L] | ||
174 | |||
175 | RewriteRule .* http://localhost:8080%{REQUEST_URI} [P,L] | ||
176 | ProxyPassReverse / http://localhost:8080/ | ||
177 | {{/code}} | ||
178 | |||
179 | {{warning}}Note that while this has been tested to work with XWiki 3.1, it seems it [[doesn't work with 4.5.4 and later>>http://jira.xwiki.org/browse/XWIKI-9455]].{{/warning}} | ||
180 | |||
181 | == Lighttpd + Jetty == | ||
182 | |||
183 | 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)): | ||
184 | |||
185 | {{code language="none"}} | ||
186 | $HTTP["host"] =~ "^www\.domain\.com$" { | ||
187 | # ensure all requests for .gwtrpc files go through to java server | ||
188 | # we can put this rule first as a higher priority, which java couldn't do | ||
189 | $HTTP["url"] =~ "\.gwtrpc$" { | ||
190 | proxy.server = ( "" => (( "host" => "127.0.0.1", "port" => 8080 ))) | ||
191 | } | ||
192 | # otherwise, we can handle the static resources | ||
193 | else $HTTP["url"] =~ "^/resources/" { | ||
194 | alias.url += ( "/resources" => "/usr/share/jetty/webapps/root/resources" ) | ||
195 | } | ||
196 | # otherwise, we can handle the static resources | ||
197 | else $HTTP["url"] =~ "^/skins/" { | ||
198 | alias.url += ( "/skins" => "/usr/share/jetty/webapps/root/skins" ) | ||
199 | } | ||
200 | # and here is the primary server | ||
201 | else $HTTP["host"] =~ "^www\.domain\.com$" { | ||
202 | proxy.server = ( "" => (( "host" => "127.0.0.1", "port" => 8080 ))) | ||
203 | } | ||
204 | } | ||
205 | # redirect anything.domain.com to www.domain.com | ||
206 | else $HTTP["host"] =~ "\.domain\.com$" { | ||
207 | url.redirect = ( "^/(.*)" => "http://www.domain.com/$1" ) | ||
208 | server.name = "www.domain.com" | ||
209 | } | ||
210 | # redirect domain.com to www.domain.com | ||
211 | else $HTTP["host"] =~ "domain\.com$" { | ||
212 | url.redirect = ( "^/(.*)" => "http://www.domain.com/$1" ) | ||
213 | server.name = "www.domain.com" | ||
214 | } | ||
215 | {{/code}} | ||
216 | |||
217 | So lighttpd will serve any static content unless it has ##.gwtrpc## on the end of the URL. | ||
218 | |||
219 | If you use Nginx as a web-server, just add three more locations and set ##root## to them. By ##try_files## Nginx checks static content presence and if doesn't exist, redirect it to the Tomcat (we expect dynamic content in this case, including all ##*.gwtrpc## requests). | ||
220 | |||
221 | {{code}} | ||
222 | location /skins/ { | ||
223 | root /var/lib/tomcat7/webapps/ROOT; | ||
224 | } | ||
225 | |||
226 | location /resources/ { | ||
227 | try_files $uri $uri/ @fallback; | ||
228 | root /var/lib/tomcat7/webapps/ROOT; | ||
229 | } | ||
230 | |||
231 | location @fallback { | ||
232 | proxy_pass http://localhost:8080; | ||
233 | } | ||
234 | {{/code}} | ||
235 | |||
236 | In the example above XWiki installed as ROOT application in Tomcat. Change path to your XWiki application accordingly. | ||
237 | |||
238 | Then in ##web.xml##, I changed the ##gwtrpc## mapping to: | ||
239 | |||
240 | {{code language="xml"}} | ||
241 | <servlet-mapping> | ||
242 | <servlet-name>gwtrpc</servlet-name> | ||
243 | <url-pattern>/resources/*</url-pattern> | ||
244 | <url-pattern>/skins/*</url-pattern> | ||
245 | </servlet-mapping> | ||
246 | {{/code}} | ||
247 | |||
248 | 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. | ||
249 | |||
250 | Now, as described above, add a rule to catch everything else and redirect it to your XWiki servlet: | ||
251 | |||
252 | {{code language="xml"}} | ||
253 | <servlet-mapping> | ||
254 | <servlet-name>action</servlet-name> | ||
255 | <url-pattern>/*</url-pattern> | ||
256 | </servlet-mapping> | ||
257 | {{/code}} | ||
258 | |||
259 | = III. Struts action name = | ||
260 | |||
261 | 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. | ||
262 | |||
263 | 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}}. | ||
264 | |||
265 | = IV. Error Page = | ||
266 | |||
267 | At the ##WEB-INF/web.xml##, the ##location## of the 404 error code needs to be changed accordingly. For example: | ||
268 | |||
269 | {{code language="xml"}} | ||
270 | <error-page> | ||
271 | <error-code>404</error-code> | ||
272 | <!--<location>/xwiki/bin/view/Main/DocumentDoesNotExist</location>--> | ||
273 | <location>/bin/Main/DocumentDoesNotExist</location> | ||
274 | </error-page> | ||
275 | {{/code}} | ||
276 | |||
277 | = V. Conclusion = | ||
278 | |||
279 | After performing all these changes, you should be able to access documents with URLs like: | ||
280 | |||
281 | * server.com/Space/Document | ||
282 | * server.com/Space/ (pointing to Space.WebHome) | ||
283 | * server.com/Document (pointing to Main.Document) | ||
284 | * server.com/ will show Main.WebHome, without any redirect. | ||
285 | |||
286 | 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. |