如何为“目录”路由设置默认文件

问题描述

例如,我可以在 .directory() 路径上为 http 服务器创建 / 路由,但它仅通过直接链接提供文件,例如 /index.html。如何让它从 /index.html 重定向/

解决方法

同样使用 .htaccess 文件。

Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain.co.uk [NC]
RewriteRule ^(.*)$ http://www.domain.co.uk/$1 [R=301,NC]
RewriteRule ^index.html$ http://www.domain.co.uk/ [R=301,L]
,

经过一些实验,我找到了对我有用的解决方案。

routes.file("/",destinationDir + File.separator + "index.html");
routes.directory("/",Paths.get(destinationDir);

这是因为在文件的情况下,路由器需要完全匹配的路径,并且在执行目录前缀比较的情况下。