大网数据广告活动
服务器运维问答

htaccess - Allow viewing of index.html via www.example.com/foo instead of www.example.com/foo/index.html

· 52CE 我爱测

So I have a hypothetical folder on my website protected by Deny from all on my .htaccess file. I have coded an exception as below: <files index.html> order allow,deny Allow from all </files> If a user types www.example.com/path/i…

问题原文

原帖链接 · hjk321 · CC BY-SA 4.0

So I have a hypothetical folder on my website protected by Deny from all on my .htaccess file. I have coded an exception as below:

<files index.html>
order allow,deny
Allow from all
</files>

If a user types www.example.com/path/index.html they are able to see the webpage just fine. However, if they go by the more "user-friendly" approach and type www.example.com/path they are given a 403 Forbidden error. Since both paths are accessing the same file, I fail to see how one would work and another would not. There are no other index files in the directory that could potentially be interfering with index.html. Is there a way to configure .htaccess in a way that allows this second path? Any solution is acceptable, including ones outside the realm of .htaccess; yes I have PHP, no I do not have AJAX.

已采纳解答

原帖链接 · MrWhite · CC BY-SA 4.0

When you specify just the bare directory mod_dir issues an internal subrequest for the DirectoryIndex (which I assume is configured to serve index.html in your case). "The problem" is that the <Files> directive is first processed before the subrequest occurs. But before this subrequest occurs the filename that the <Files> directive matches against has not yet been resolved; it is empty! So, we need to match against an empty filename.

However, once the subrequest for index.html (the DirectoryIndex) has occurred then the <Files> container is reprocessed (in a .htaccess context), but this time the filename has resolved to index.html. So, we need to match against index.html as well!

This can be accounted for by either having two <Files> containers. For example:

<Files "">
Order allow,deny
Allow from all
</Files>

<Files "index.html">
Order allow,deny
Allow from all
</Files>

Or (preferably) combining these into a single <FilesMatch> container (that accepts a regex as the argument). For example:

<FilesMatch ^(index\.html)?$>
Order allow,deny
Allow from all
</Files>

By making the filename optional (trailing ?) this effectively matches both passes: an empty filename and index.html.

Note that if both the URLs / and /index.html are available and serve the same content then you should canonicalise the URL in some way to avoid potential duplicate content issues. (Preferably a redirect from /index.html to /.)


...and type www.example.com/path

Just to clarify, if the user types www.example.com/path, where path is a filesystem directory and a trailing slash is omitted from the URL, then mod_dir will (by default) issue an external 301 redirect to www.example.com/path/ (with a trailing slash) in order to "fix" the URL. So the URL we are dealing with is really www.example.com/path/.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-05-28)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。