本教程介绍了如何调整XWiki平台,用更短的URL方案替换默认URL方案。
短网址既url去除xwiki/bin/view的部分。
I. 应用程序名称
URL的/xwiki/部分是应用程序的名称。它识别了应该处理请求的应用程序,它允许一个容器托管多个应用程序。要修改它,你必须参考你的容器的文档,并找到如何映射Web应用程序的上下文路径。例如在Tomcat可以在webapps目录下很轻松部署XWiki,并在webapps子目录下命名你想使用的应用程序名称。
以ROOT部署
一个特例是在部署XWiki时作为ROOT application,它实际上是允许应用程序名称的部分是空的,这样URL可以为server.com/bin/view/Space/Document。
实现这一点要依赖于容器,因为没有关于ROOT application的标准。
一些例子:
- 在Tomcat中,在默认配置下,所需要的是在webapps并在其下名为ROOT的子目录部署XWiki Web应用程序(即 webapps/ROOT)。
- 在Jetty中,在默认配置下,所需要的是在webapps并在其下名为root的子目录部署XWiki Web应用程序。请注意,如果你使用的是打包的zip安装的xwiki(包含jetty和HSQLDB),那么你还需要:
- 除去现有的webapps/root目录
- 把现有的webapps/xwiki目录重命名为webapps/root。
- 去除jetty/contexts/xwiki.xml文件,只保留jetty/contexts/root.xml。否则,会在控制台出现警告。
此外,从XWiki 6.2.8/6.4.3/7.0开始,当以ROOT部署,你必须到xwiki.cfg配置文件设置xwiki.webapppath为空,如下所示:
xwiki.webapppath=
II. Servlet映射名称
第二部分是最难删除的部分。它确定了应该处理页面的servlet,如/bin/,既Struts servlet。一般地讲,摆脱/bin/,你需要配置你的系统使得URL匹配/*映射到Struts Servlet(默认情况下只有URL为/bin/*才映射Struts Servlet)。
但是你必须要小心,以下前缀不能通过Struts Servlet(查看web.xml检查他们的当前映射):
- /resources/* 和/skins/*:静态的资源。这些都需要直接作为静态资源服务。
- /rest/*: REST资源,服务于XWiki REST Servlet REST
- /xmlrpc/*: XML-RPC资源,服务与XWiki XMLRPC Servlet
- /resources/**/*.gwtrpc: GWT-RPC调用, 服务于XWiki GWT Servlet
- /webdav/*: WebDav调用, 服务于XWiki WebDAV Servlet
- /XWikiService: 其他XWiki GWT Servlet
- /redirect:XWiki Redirect Servlet,当URL没有指定的页面,重定向到主页
有以下替代方式来实现:
- UrlRewriteFilter
- Apache
- 其他
UrlRewriteFilter
这是最简单的解决办法,但是需要XWiki 5.2+ 以上的版本,因为在这个5.2版本这个解决一个BUG。
“UrlRewriteFilter”是一个框架提供一个Servlet过滤器,允许重写URL。
安装步骤:
- 下载JAR并放在WEB-INF/lib
- 编辑WEB-INF/web.xml并添加<filter>和filter-mapping ,可查看文档
- 拖拉以下内容到WEB-INF/urlrewrite.xml:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 4.0//EN"
"http://www.tuckey.org/res/dtds/urlrewrite4.0.dtd">
<urlrewrite decode-using="null">
<rule>
<note>
Ensure that URLs ending with .gwtrpc are not modified.
</note>
<from>^/(.*)\.gwtrpc$</from>
<to type="forward" last="true">-</to>
</rule>
<rule>
<note>
Ensure that URLs that must not be served by the Struts Servlet are not modified.
</note>
<from>^/(bin|resources|skins|rest|webdav|xmlrpc|wiki|webjars)/(.*)$</from>
<to type="forward" last="true">-</to>
</rule>
<rule>
<note>
For all other URLs we prepend the "/bin/" prefix so that they get routed to the XWiki Action Servlet.
</note>
<from>^/(.*)$</from>
<to type="forward">/bin/$1</to>
</rule>
<outbound-rule>
<note>
Rewrite outbound URLs to remove the "/bin" part when there are two paths after it.
</note>
<from>/bin/(.*)/(.*)$</from>
<to>/$1/$2</to>
</outbound-rule>
<outbound-rule>
<note>
Rewrite outbound URLs to remove the "/bin" part when there's a single path after it.
</note>
<from>/bin/(.*)$</from>
<to>/$1</to>
</outbound-rule>
<outbound-rule>
<note>
Rewrite outbound URLs to remove the "/bin" part it's the last path.
</note>
<from>/bin$</from>
<to>/</to>
</outbound-rule>
</urlrewrite>
Apache
策略:
- 告诉Apache /skins和/resources的URL(除了/resources/**/*.gwtrpc)服务静态资源,使他们不通过Servlet容器
- 配置的web.xml使/*URL通过Struts Servlet和使/resources/**/*.gwtrpcURL通过GWT Servlet
- 告诉XWiki不产生带有bin的URL路径
配置步骤:
- 设置以下Apache配置:
Alias /skins /usr/local/xwiki/skins
Alias /resources /usr/local/xwiki/resources
RewriteEngine on
RewriteRule ^/+skins - [L]
RewriteCond %{REQUEST_URI} !\.gwtrpc$
RewriteRule ^/+resources($|/.*) - [L]
RewriteRule .* http://localhost:8080%{REQUEST_URI} [P,L]
ProxyPassReverse / http://localhost:8080/ - 编辑web.xml文件并添加:
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping> - 编辑web.xml并替换现有的映射:
<servlet-mapping>
<servlet-name>gwtrpc</servlet-name>
<url-pattern>*.gwtrpc</url-pattern>
</servlet-mapping>如
<servlet-mapping>
<servlet-name>gwtrpc</servlet-name>
<url-pattern>/resources/*</url-pattern>
</servlet-mapping> - 在xwiki.cfg文件里添加以下内容 (等号后面为空值):
xwiki.defaultservletpath=
- 仅适用于XWiki 5.2+在xwiki.properties文件添加以下内容 (等号后面为空值):
url.standard.getEntityPathPrefix=
在XWiki 5.1版本这么做会有问题。我们建议您升级到XWiki 5.2。如果你没办法升级,可以按以下几点来处理:
- 去除在WEB-INF/lib目录下3个xwiki-platform-url-*JAR包并且替换为以下jar包:
- http://maven.xwiki.org/releases/org/xwiki/platform/xwiki-platform-url-api/5.2-milestone-2/xwiki-platform-url-api-5.2-milestone-2.jar
- http://maven.xwiki.org/releases/org/xwiki/platform/xwiki-platform-url-container/5.2-milestone-2/xwiki-platform-url-container-5.2-milestone-2.jar
- http://maven.xwiki.org/releases/org/xwiki/platform/xwiki-platform-url-standard/5.2-milestone-2/xwiki-platform-url-standard-5.2-milestone-2.jar
- 编辑xwiki.properties文件并添加以下内容 (等号后面为空值):
url.standard.entityPathPrefix=
其他
以下是XWiki使用者贡献的解决方案,不过没有经过XWiki开发团队的检验,如果你使用,可能会带来一些风险
III. Struts action名称
第三部分, /view/, 定义了一个处理请求的struts action。因此,这告诉我们想要对文件的操作,如/view/ , /edit/或者/delete/。
可以让XWIki忽略view action。通过编辑xwiki.cfg文件并设置xwiki.showviewaction=0。
XWiki 7.2之前,支持不通过更改配置来忽略view。然而,在7.2版本为了保证 Nested Pages功能,需要通过配置文件修改。
IV. 错误页面
位于WEB-INF/web.xml文件下,404错误页面也需要相应的修改。如下:
<error-page>
<error-code>404</error-code>
<!--<location>/xwiki/bin/view/Main/DocumentDoesNotExist</location>-->
<location>/bin/Main/DocumentDoesNotExist</location>
</error-page>
V. 结论
执行所有这些改变后,你应该能够通过以下url访问文档:
- server.com/Space/Document
- server.com/Space/ (指向Space.WebHome)
- server.com/Document (指向Main.Document)
- server.com/ 将显示Main.WebHome,没有任何重定向。
这些变化都是向后兼容的,这意味着既是执行完这些变化也不会影响到当前工作中的URL。