优秀的编程知识分享平台

网站首页 > 技术文章 正文

避免网站中出现 index.html(如何才能避免网页中出现乱码?)

nanyue 2024-10-21 06:20:56 技术文章 11 ℃

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

1、保持简洁优雅的 URL。

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

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

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

开始设置

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

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

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

确保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 <link> 标记,避免在搜索引擎出现 index.html

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

    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=5, viewport-fit=cover">
         ...
         <link rel="canonical" href="https://www.fuyeor.com" />
         ...
    </head>

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

Tags:

最近发表
标签列表