为什么有些网站在文件扩展名末尾没有显示.php?

时间:2013-04-20 01:10:15

标签: php html url

有时,当我查看网站网址时,会说出类似这样的内容

www.website.com/index.php

有时它看起来像这样

www.website.com/index /

为什么第二个网址没有.php或.html扩展名?

1 个答案:

答案 0 :(得分:5)

在正常配置的Web服务器中,

www.website.com/index.php

告诉它从webroot目录加载index.php文件。此

www.website.com/index/

告诉它在 / index / 目录中查找与服务器 httpd.conf中的 DirectoryIndex 配置指令中列出的文件匹配的文件文件或网站webroot目录中的 .htaccess 文件(有关详细信息,请参阅http://httpd.apache.org/docs/2.2/mod/mod_dir.html#directoryindex)。

示例配置指令如下所示:

DirectoryIndex index.php index.html default.html default.htm

这会告诉Web服务器,当没有给出文件名时,在当前目录中查找 index.php 文件,如果找不到,则查找 index.html < / strong>文件,如果没有找到 default.html 文件,依此类推其他所有内容。

如果您希望能够运行PHP文件而不必在其末尾使用 .php ,则可以在配置文件中设置默认处理程序(httpd.conf或.htaccess)如:

ForceType application/x-httpd-php 

这将告诉Apache将所有文件作为PHP处理,无论它们是否具有 .php 扩展名。 (见http://httpd.apache.org/docs/2.2/mod/core.html#forcetype

如果您愿意,也可以使用ModRewrite重新映射URL,但ModRewrite指令可能会令人困惑且难以排除故障。