将 index.html 设为默认值,但如果输入,则允许访问 index.php

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10002439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 23:31:44  来源:igfitidea点击:

Make index.html default, but allow index.php to be visited if typed in

html.htaccess

提问by Matt Rowles

I have the following line in my .htaccess file:

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

DirectoryIndex index.html index.php

Everytime I go to index.php it takes me to index.html. Is it possible to allow for both, but leave index.html the default for users visiting www.domain.com?

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

回答by The Alpha

By default, the DirectoryIndex is set to:

默认情况下,DirectoryIndex 设置为:

DirectoryIndex index.html index.htm default.htm index.php index.php3 index.phtml index.php5 index.shtml mwindex.phtml

Apache will look for each of the above files, in order, and serve the first one it finds when a visitor requests just a directory. If the webserver finds no files in the current directory that match names in the DirectoryIndex directive, then a directory listing will be displayed to the browser, showing all files in the current directory.

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

The order should be DirectoryIndex index.html index.php// default is index.html

顺序应该是DirectoryIndex index.html index.php// 默认是 index.html

Reference:Here.

参考:这里

回答by Natacha Beaugeais

If you're using WordPress, there is now a filter hook to resolve this:

如果您使用的是 WordPress,现在有一个过滤器钩子可以解决这个问题:

remove_filter('template_redirect', 'redirect_canonical'); 

(Put this in your theme's functions.php)

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

This tells WordPress to not redirect index.phpback to the root page, but to sit where it is. That way, index.htmlcan be assigned to be the default page in .htaccessand can work alongside index.php.

这告诉 WordPress 不要重定向index.php回根页面,而是坐在它所在的位置。这样,index.html可以指定为默认页面.htaccess并可以与index.php.

回答by starkeen

I agree with @TheAlpha's accepted answer, Apache reads the DirectoryIndex target files from left to right , if the first file exists ,apche serves it and if it doesnt then the next file is served as an index for the directory. So if you have the following Directive :

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

DirectoryIndex file1.html file2.html

Apache will serve /file.html as index ,You will need to change the order of files if you want to set /file2.html as index

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

DirectoryIndex file2.html file1.html

You can also set index file using a RewriteRule

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

RewriteEngine on

RewriteRule ^$ /index.html [L]

RewriteRule above will rewrite your homepage to /index.html the rewriting happens internally so http://example.com/would show you the contents ofindex.html .

上面的 RewriteRule 会将您的主页重写为 /index.html 重写在内部发生,因此http://example.com/会向您显示 index.html 的内容。

回答by xgqfrms

DirectoryIndex index.html index.htm default.htm index.php index.php3 index.phtml index.php5 index.shtml mwindex.phtml

DirectoryIndex index.html index.htm default.htm index.php index.php3 index.phtml index.php5 index.shtml mwindex.phtml

it doesn't has any means?you may be just need to add like this!

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

<IfModule dir_module>
    DirectoryIndex index.php index.html index.htm
</IfModule>

enter image description here

在此处输入图片说明

回答by xgqfrms

RewriteEngine on
RewriteRule ^(.*)\.html$ .php%{QUERY_STRING} [L]

Put these two lines at the top of your .htaccess file. It will show .html in the URL for your .php pages.

将这两行放在 .htaccess 文件的顶部。它将在 .php 页面的 URL 中显示 .html。

RewriteEngine on
RewriteRule ^(.*)\.php$ .html%{QUERY_STRING} [L]

Use this for showing .php in URL for your .html pages.

用于在 .html 页面的 URL 中显示 .php。

回答by Jodyshop

Hi,

你好,

Well, I have tried the methods mentioned above! it's working yes, but not exactly the way I wanted. I wanted to redirect the default page extensionto the main domain with our further action.

嗯,上面的方法我都试过了!它工作正常,但不完全是我想要的方式。我想通过我们的进一步操作将默认页面扩展名重定向到主域。

Here how I do that...

在这里我如何做到这一点...

# Accesible Index Page
<IfModule dir_module>
 DirectoryIndex index.php index.html
 RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.(html|htm|php|php3|php5|shtml|phtml) [NC]
 RewriteRule ^index\.html|htm|php|php3|php5|shtml|phtml$ / [R=301,L]
</IfModule>

The above code simply captures any index.* and redirect it to the main domain.

上面的代码简单地捕获任何 index.* 并将其重定向到主域。

Thank you

谢谢