怎么避免网站中出现 index.html

作者:玥言
链接:
https://zhuanlan.zhihu.com/p/593377190

来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

为什么要去除首页文件 index.html?

1、保持简洁优雅的 URL。

保持网站首页简洁的域名格式。

2、搜索引擎友好,避免重复页面。

虽然大部分网站首页都是 index.html,但搜索引擎并不会将www.fuyeor.com/和www.fuyeor.com/index.html视为同一个页面,而是当作两个网址处理。

开始设置

1、设置网站目录文件默认值

从 Apache 服务器的配置文件 /conf/httpd.conf 中的 DirectoryIndex 修改网站默认首页。


     DirectoryIndex index.html index.php

确保www.fuyeor.com/和www.fuyeor.com/index.html都能正常访问。

2、在首页文件 index.html 中设置跳转

在首页文件 index.html 中通过 JavaScript 设置跳转,跳转至不带 index.html 的域名。

<script>

if(window.location.href == 'https://www.fuyeor.com/index.html'){
    window.location.href="https://www.fuyeor.com/";
}

</script>

3、设置 rel=canonical 标记,避免在搜索引擎出现 index.html

将具有 rel="canonical" 属性的 元素添加到带 index.html 的网页 部分中,并使其指向规范网页(主域名)。

    
        
        
         ...
         
         ...
    

这样搜索引擎即使发现了 index.html 的网址,也不会收录。