Wiki source code of Short URLs

Last modified by Vincent Massol on 2024/03/27

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
Vincent Massol 47.1 15 == Deploying as ROOT ==
16
Vincent Massol 46.1 17 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##.
Guillaume Fenollar 43.1 18
Vincent Massol 46.1 19 Achieving this depends on the container, as there's no standard regarding the ROOT application (Refer to your container's documentation for more details).
20
21 Some examples:
Paul Libbrecht 51.1 22
Vincent Massol 46.1 23 * 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##).
Vincent Massol 26.1 24 * 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:
25 ** 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.
26 ** Rename the existing ##webapps/xwiki## directory into ##webapps/root##.
27 ** 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.
Vincent Massol 10.2 28
Vincent Massol 47.2 29 In addition, starting with XWiki 6.2.8/6.4.3/7.0 you must tell XWiki that it's deployed as ROOT by setting the ##xwiki.webapppath## to empty in your ##xwiki.cfg## configuration file as in:
Vincent Massol 26.1 30
Vincent Massol 46.1 31 {{code language="none"}}
32 xwiki.webapppath=
33 {{/code}}
34
35 The reason is that XWiki cannot guess the webapp context from the URL in this case. This seemed to work on previous versions but it was actually leading to errors from time to time, depending on what URL was used when doing the first request on the XWiki instance.
36
Vincent Massol 49.1 37 === When using the XWiki Debian Package ===
38
Ecaterina Moraru (Valica) 61.1 39 See [[Installation using Debian (.DEB) packages>>Documentation.AdminGuide.Installation.InstallationViaAPT.WebHome||anchor="HXWikiasrootwebapp28shortURLs29"]].
Vincent Massol 49.1 40
Dmitry Bakbardin 20.1 41 = II. Servlet mapping name =
maalej 1.1 42
Thomas Mortagne 71.1 43 The second part is the hardest part to remove. It identifies the servlet that should process the page, which, for ##/bin/##, is the action servlet. Generically speaking, to get rid of ##/bin/##, you need to configure your system so that URLs matching ##/*## are mapped to the Action Servlet (by default only ##/bin/*## URLs are mapped to the Action Servlet).
maalej 1.1 44
Thomas Mortagne 70.1 45 However you need to be careful that the following prefixes do NOT go through the Action Servlet (see your ##web.xml## to check their current mappings):
Guillaume Fenollar 43.1 46
Vincent Massol 29.1 47 * ##/resources/*## and ##/skins/*##: Statically served resources. These need to be served directly as static resources.
Vincent Massol 35.1 48 * ##/rest/*##: REST resources, served by the XWiki REST Servlet
Vincent Massol 29.1 49 * ##/redirect##: The XWiki Redirect Servlet used to redirect to the home page when no page is specified in the URL
maalej 1.1 50
Vincent Massol 38.1 51 There are various alternate to achieve this:
Dmitry Bakbardin 20.1 52
Vincent Massol 38.1 53 {{toc scope="local"/}}
54
Vincent Massol 37.1 55 {{info}}
Ecaterina Moraru (Valica) 62.1 56 You might be tempted to configure just your XWiki's ##web.xml## file [[but this won't work>>Documentation.AdminGuide.ShortURLs.ShortURLsInvalid.WebHome]].
Vincent Massol 37.1 57 {{/info}}
58
Vincent Massol 29.1 59 == UrlRewriteFilter ==
60
slauriere 82.1 61 {{warning}}
slauriere 86.1 62 This approach requires a specific workaround configuration with Tomcat 9.x+ (the workaround was not needed with Tomcat 8.x) since Tomcat performs very early mapping of incoming requests before the ##URLRewriteFilter## has a chance to add the ##/bin## prefix. Thus the mapping is [[wrongly associated to the default Servlet instead of the XWiki Action Servlet>>https://jira.xwiki.org/browse/XWIKI-19444?focusedCommentId=112420&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-112420]]. The workaround consists of the two actions below.
Vincent Massol 29.1 63
slauriere 94.1 64 * Declare new Servlet mappings in ##web.xml##:(((
Vincent Massol 81.1 65 {{code language="xml"}}
66 <servlet-mapping>
67 <servlet-name>action</servlet-name>
68 <url-pattern>/upload/*</url-pattern>
69 </servlet-mapping>
slauriere 94.1 70
71 <!-- This is for the file upload from CKEditor to work, since it sends a POST action to the current page using the "/get/" path -->
72 <servlet-mapping>
73 <servlet-name>action</servlet-name>
74 <url-pattern>/get/*</url-pattern>
75 </servlet-mapping>
Vincent Massol 81.1 76 {{/code}}
slauriere 85.1 77 )))
Vincent Massol 88.2 78 * Update the ##upload.js## content:
79 ** Do a backup of ##$XWIKI_WEBAPP/resources/uicomponents/widgets/upload.min.js##, then remove the original file
80 ** Edit the file ##$XWIKI_WEBAPP/resources/uicomponents/widgets/upload.js## and update the line {{code language="javascript"}}request.open('POST', this.formData.action);{{/code}} to:(((
slauriere 87.1 81 {{code language="javascript"}}
slauriere 88.1 82 request.open('POST', this.formData.action + '?form_token='+this.formData.additionalFields['form_token']
83 + '&xredirect=' + escape(this.formData.additionalFields['xredirect']));
slauriere 85.1 84 {{/code}}
85 )))
86 {{/warning}}
slauriere 84.1 87
Vincent Massol 81.1 88 "UrlRewriteFilter" is a [[framework offering a Servlet Filter>>http://www.tuckey.org/urlrewrite/]] allowing to rewrite URLs.
Vincent Massol 36.1 89
90 Install steps:
Guillaume Fenollar 43.1 91
Vincent Massol 73.2 92 1. [[Download the JAR>>https://search.maven.org/artifact/org.tuckey/urlrewritefilter]] and put it in ##WEB-INF/lib##
Vincent Massol 64.1 93 1. Edit your ##WEB-INF/web.xml## and add the ##<filter>## and ##<filter-mapping>## definitions [[as documented>>http://www.tuckey.org/urlrewrite/]]. Make sure to have them first so that they're executed before any other XWiki filter.
slauriere 67.1 94 1. Drop the following content below in a file at ##WEB-INF/urlrewrite.xml##:(((
Vincent Massol 63.1 95 Note that this is just an example and you should tune it depending on what you wish to achieve. Its principle is very simple: it takes some URL format you wish to have and convert those URLs into the format that XWiki expects in input, and, in output, it does the opposite and converts the XWiki format into the format you wish to have.
96
97 The example below expects URLs without a ##/bin## and thus adds it so that XWiki has it when it processes URLs and in output, removes the ##/bin## so that generated URLs are without ##/bin##.
98
Jerome 14.1 99 {{code language="xml"}}
Vincent Massol 29.1 100 <?xml version="1.0" encoding="utf-8"?>
101 <!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 4.0//EN"
102 "http://www.tuckey.org/res/dtds/urlrewrite4.0.dtd">
Vincent Massol 42.1 103 <urlrewrite decode-using="null">
Vincent Massol 29.1 104
105 <rule>
106 <note>
Thomas Mortagne 70.1 107 Ensure that URLs that must not be served by the Action Servlet are not modified.
Vincent Massol 29.1 108 </note>
slauriere 83.1 109 <from>^/((authenticate|bin|resources|skins|asyncrenderer|rest|wiki|jcaptcha|webjars|job)/(.*)|robots\.txt)$</from>
Vincent Massol 29.1 110 <to type="forward" last="true">-</to>
111 </rule>
112
113 <rule>
114 <note>
115 For all other URLs we prepend the "/bin/" prefix so that they get routed to the XWiki Action Servlet.
116 </note>
117 <from>^/(.*)$</from>
118 <to type="forward">/bin/$1</to>
119 </rule>
120
Vincent Massol 32.1 121 <outbound-rule>
122 <note>
Vincent Massol 48.1 123 Rewrite outbound URLs to remove the "/bin" part when there are two paths after it.
Vincent Massol 32.1 124 </note>
125 <from>/bin/(.*)/(.*)$</from>
126 <to>/$1/$2</to>
127 </outbound-rule>
128
Vincent Massol 48.1 129 <outbound-rule>
130 <note>
131 Rewrite outbound URLs to remove the "/bin" part when there's a single path after it.
132 </note>
133 <from>/bin/(.*)$</from>
134 <to>/$1</to>
135 </outbound-rule>
136
137 <outbound-rule>
138 <note>
139 Rewrite outbound URLs to remove the "/bin" part it's the last path.
140 </note>
141 <from>/bin$</from>
142 <to>/</to>
143 </outbound-rule>
144
Vincent Massol 29.1 145 </urlrewrite>
146 {{/code}}
Vincent Massol 36.1 147 )))
Vincent Massol 29.1 148
Vincent Massol 94.2 149 **Note**: for debugging of URL Rewrite filter, set the ##logLevel## parameter as described in https://tuckey.org/urlrewrite/manual/5.1/index.html#filterparams (using the value ##sysout:DEBUG##, the logs can be read in tomcat logs).
Anca Luca 45.1 150
Vincent Massol 39.1 151 == Apache ==
Jerome 16.1 152
Vincent Massol 39.1 153 Strategy:
Guillaume Fenollar 43.1 154
Vincent Massol 73.1 155 * Tell Apache that ##/skins## and ##/resources## URLs are served statically so that they don't go through the Servlet container
156 * Configure ##web.xml## so that ##/*## URLs go through the Action Servlet
Vincent Massol 39.1 157 * Tell XWiki to not generate URLs with ##bin## in the path
Sergiu Dumitriu 4.1 158
Vincent Massol 39.1 159 Configuration steps:
Guillaume Fenollar 43.1 160
Vincent Massol 39.1 161 * Setup the following Apache configuration:(((
162 {{code language="apache"}}
Manuel Smeria 19.4 163 Alias /skins /usr/local/xwiki/skins
164 Alias /resources /usr/local/xwiki/resources
Jerome 19.1 165
Vincent Massol 34.1 166 RewriteEngine on
167
168 RewriteRule ^/+skins - [L]
169 RewriteCond %{REQUEST_URI} !\.gwtrpc$
170 RewriteRule ^/+resources($|/.*) - [L]
171
Clément Aubin 89.1 172 ## For some reason, the "/" end points does not work with tomcat, so the workaround is to rewrite it to /Main
173 RewriteRule ^/*$ http://localhost:8080/Main/ [P,L]
174 ## For all other URLs, we go to tomcat
Vincent Massol 34.1 175 RewriteRule .* http://localhost:8080%{REQUEST_URI} [P,L]
176 ProxyPassReverse / http://localhost:8080/
177 {{/code}}
Vincent Massol 39.1 178 )))
Vincent Massol 41.1 179 * Edit ##web.xml## and add:(((
Vincent Massol 39.1 180 {{code language="xml"}}
181 <servlet-mapping>
182 <servlet-name>action</servlet-name>
183 <url-pattern>/*</url-pattern>
184 </servlet-mapping>
185 {{/code}}
186 )))
187 * Add the following in ##xwiki.cfg## (empty value after the equal sign):(((
Jerome 14.1 188 {{code language="none"}}
Vincent Massol 39.1 189 xwiki.defaultservletpath=
Jerome 14.1 190 {{/code}}
Guillaume Fenollar 43.1 191 )))
PaulHarris 12.1 192
Thomas Mortagne 70.1 193 = III. action name =
Jerome 14.1 194
Alex Cotiugă 93.1 195 The third part, ##/view/##, identifies the 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.
Manuel Smeria 19.4 196
Alex Cotiugă 93.1 197 It's possible to configure XWiki to allow omitting this action when you wish to imply ##view## and also to have XWiki generate URLs without the ##view## action. This is achieved by editing ##xwiki.cfg## and setting {{code language="none"}}xwiki.showviewaction=0{{/code}}.
Manuel Smeria 19.4 198
Vincent Massol 50.2 199 {{info}}
Alex Cotiugă 93.1 200 Before XWiki 7.2 and Nested Pages, XWiki was supporting dropping ##view## in the URL without changing the configuration. However this was broken in 7.2 in order to support Nested Pages.
Vincent Massol 50.2 201 {{/info}}
202
Dmitry Bakbardin 20.1 203 = IV. Error Page =
Manuel Smeria 19.4 204
Jerome 14.1 205 At the ##WEB-INF/web.xml##, the ##location## of the 404 error code needs to be changed accordingly. For example:
206
207 {{code language="xml"}}
208 <error-page>
mingfai 9.1 209 <error-code>404</error-code>
210 <!--<location>/xwiki/bin/view/Main/DocumentDoesNotExist</location>-->
211 <location>/bin/Main/DocumentDoesNotExist</location>
212 </error-page>
Jerome 14.1 213 {{/code}}
mingfai 9.1 214
Dmitry Bakbardin 20.1 215 = V. Conclusion =
Sergiu Dumitriu 4.1 216
217 After performing all these changes, you should be able to access documents with URLs like:
Jerome 14.1 218
Sergiu Dumitriu 4.1 219 * server.com/Space/Document
220 * server.com/Space/ (pointing to Space.WebHome)
221 * server.com/Document (pointing to Main.Document)
222 * server.com/ will show Main.WebHome, without any redirect.
223
Vincent Massol 5.1 224 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.
Valdis Vitolins 54.1 225
226
227 = Other solutions =
228
229 Some XWiki users have contributed other solutions but they've not been verified by a XWiki Development Team member at this stage so use at your own risk ;)
230
Thomas Mortagne 91.1 231 * [[Lighttpd + Jetty>>dev:Drafts.ShortURLsLighttpdJetty]]
Valdis Vitolins 58.1 232 * [[Apache + Tomcat>>http://odo.lv/Recipes/XWikiShortURLs?language=en]]

Get Connected