使用 mod_rewrite 在 Apache 中创建自定义目录索引
所以,Apache 的默认目录列表很糟糕,mod_autoindex 的自定义选项也很糟糕,所以我想我可以在我的服务器根目录中编写一些重写规则,以将任何请求重定向到目录:
<IfModule rewrite_module>
RewriteEngine on
RewriteCond %{REQUEST_URI} -d
RewriteRule .* /index.php?dir=$0
</IfModule>
现在,我觉得这至少有一点效果,因为关闭indexes
仍然可以访问 localhost
(它显示index.php),但是它似乎无法到达子目录(如果 indexes
选项打开,或者如果关闭则给出 403
)。
我的问题是:我可以让这个规则在全球范围内应用吗?或者我对漂亮目录索引的追求注定会失败吗?
编辑:我应该注意:上述规则包含在服务器根目录中的.htaccess
中。
编辑:理想情况下,解决方案(如果存在)仍将具有 DirectoryIndex
功能(即,如果存在,将显示 index.php
等)存在),
So, Apache's default directory listing sucks and mod_autoindex's customisation options suck, so I figured I could write some rewrite rules in my server root to redirect any requests to a directory:
<IfModule rewrite_module>
RewriteEngine on
RewriteCond %{REQUEST_URI} -d
RewriteRule .* /index.php?dir=$0
</IfModule>
Now, I get the impression this works at least a little, as turning off indexes
still leaves localhost
accessible (it displays the index.php), however it doesn't seem to reach subdirectories (which either revert to Apache's directory indexes if the indexes
option is on, or give a 403
if they're off).
My question is: can I get this rule to apply globally or is my quest for pretty directory indexes doomed to failure?
Edit: I should note: the above rules are contained in a .htaccess
in the server root.
Edit: Ideally the solution, if it exists, would still have DirectoryIndex
functionality (that is, index.php
etc. will be displayed if it exists),
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
-d
目录测试仅适用于绝对文件系统路径。因此,要么提供绝对文件系统路径(例如直接使用%{DOCUMENT_ROOT}%{REQUEST_URI}
或%{REQUEST_FILENAME}
),要么使用-D 发出额外的子请求以将 URI 路径解析为文件系统路径。我更喜欢:
The
-d
directory test does only work with absolute file system paths. So either provide an absolute file system path (e.g. by using%{DOCUMENT_ROOT}%{REQUEST_URI}
or%{REQUEST_FILENAME}
directly) or use-D
that makes an additional subrequest to resolve the URI path to a file system path. I’d prefer: