使index.html成为默认值,但如果键入,则允许访问index.php

Mat*_*les 56 html .htaccess

我的.htaccess文件中有以下行:

DirectoryIndex index.html index.php
Run Code Online (Sandbox Code Playgroud)

每次我去index.php它都会把我带到index.html.是否可以同时允许两者,但将index.html保留为访问www.domain.com的用户的默认值?

The*_*pha 90

默认情况下,DirectoryIndex设置为:

DirectoryIndex index.html index.htm default.htm index.php index.php3 index.phtml index.php5 index.shtml mwindex.phtml
Run Code Online (Sandbox Code Playgroud)

Apache将按顺序查找上述每个文件,并在访问者仅请求目录时为其找到第一个文件.如果Web服务器在当前目录中找不到与DirectoryIndex指令中的名称匹配的文件,则将向浏览器显示目录列表,显示当前目录中的所有文件.

订单应该是DirectoryIndex index.html index.php//默认是index.html

参考: 这里.


Nat*_*ais 16

如果你正在使用WordPress,现在有一个过滤器钩子来解决这个问题:

remove_filter('template_redirect', 'redirect_canonical'); 
Run Code Online (Sandbox Code Playgroud)

(把它放在你的主题中functions.php)

这告诉WordPress不要重定向index.php回根页面,而是要坐在它的位置.这样,index.html可以将其指定为默认页面.htaccess并且可以同时工作index.php.


xgq*_*rms 5

DirectoryIndexindex.htmlindex.htmdefault.htmindex.phpindex.php3index.phtmlindex.php5index.shtmlmwindex.phtml

它没有任何手段吗? 您可能只需要像这样添加!

<IfModule dir_module>
    DirectoryIndex index.php index.html index.htm
</IfModule>
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


sta*_*een 5

我同意@ TheAlpha接受的答案,Apache从左到右读取DirectoryIndex目标文件,如果第一个文件存在,apche服务它,如果它没有,那么下一个文件将作为目录的索引.因此,如果您有以下指令:

DirectoryIndex file1.html file2.html
Run Code Online (Sandbox Code Playgroud)

Apache将/file.html作为索引提供,如果要将/file2.html设置为索引,则需要更改文件的顺序

DirectoryIndex file2.html file1.html
Run Code Online (Sandbox Code Playgroud)

您还可以使用RewriteRule设置索引文件

RewriteEngine on

RewriteRule ^$ /index.html [L]
Run Code Online (Sandbox Code Playgroud)

上面的RewriteRule会将您的主页重写为/index.html,内部进行重写,因此http://example.com/会显示inindex.html的内容.