![Photo by Kellen Riggin]()
阿里云 OSS 本质是对象储存,没有目录的概念,所以使用 OSS 设置静态网站托管时,地址必须存在对象文件,否则将跳转到首页。比如访问 www.abc.com/a/ ,必须指定为 www.abc.com/a/index.html
Hexo+NexT 环境下,按照以下方式修改,这样在 hexo g
之后生成的静态文件会补全路径。下文在标题中会注明改动的文件属于 Hexo
还是 NexT
,遇到版本更新时,需要再次修改相应的源文件。
写在前面
这篇文章是 2019 年写的,截至到 2020 年 9 月,阿里云 OSS 已升级 “子目录首页” 功能,开通 “子目录首页” 功能后将文件 404 规则设置为 “Index”,就可以正常访问 www.abc.com/a/,OSS 会自动读取文件夹下的 index.html
。
![OSS开启子目录首页]()
不过实测发现,当 OSS 启用 CDN 加速之后,假如更新 index.html
,OSS 的 CDN 自动刷新机制只会刷新 www.abc.com/a/index.html,而不会刷新 www.abc.com/a/
![OSS-CDN自动刷新]()
所以请根据自己站点的实际情况判断
- 如果你的 OSS 没有配置 CDN,可以在 OSS 控制面板开通 “子目录首页” 功能,不用单独为 Hexo-NexT 增加默认页
- 如果你的 OSS 已绑定 CDN 并自动刷新,需要根据以下文章内容修改源文件
更新记录
随着 Hexo、Next 版本更新,源文件的内容会发生变化,修改前请检查环境版本是否一致
日期 | 内容 |
---|
2019-01 | 初稿 |
2019-11 | 适配 NexT-7.x |
2020-09 | 适配 NexT-8.x |
Hexo 文件修改
Hexo 很少更新,所以在 Hexo 文件上的修改几乎是 “一劳永逸”,其实每次升级 Hexo 版本都会比较谨慎,升级后 Hexo s
检查下是否被覆盖。
文章地址
修改根目录下的 Hexo 配置文件 ,在 permalink
的最后加上 .html
1 2 3 4
| permalink: post/:category/:urltitle.html permalink_defaults: urltitle: :year:month:day-index
|
如果 Hexo 版本大于 5.0,将 pretty_urls
两个值设置为 true
1 2 3 4
| pretty_urls: trailing_index: true trailing_html: true
|
首页分页
修改 \node_modules\hexo\lib\plugins\helper\paginator.js
1 2 3 4 5
| const createLink = (options, ctx) => { const { base, format } = options; - return i => ctx.url_for(i === 1 ? base : base + format.replace('%d', i)); + return i => ctx.url_for(i === 1 ? base : base + format.replace('%d', i)) + 'index.html'; };
|
分类页面
修改 \node_modules\hexo\lib\plugins\helper\list_categories.js
1 2
| - const { style = 'list', transform, separator = ', ', suffix = '' } = options; + const { style = 'list', transform, separator = ', ', suffix = 'index.html' } = options;
|
标签列表
修改 \node_modules\hexo\lib\plugins\helper\list_tags.js
1 2
| - const { style = 'list', transform, separator = ', ', suffix = '' } = options; - const { style = 'list', transform, separator = ', ', suffix = 'index.html' } = options;
|
标签云
修改 \node_modules\hexo\lib\plugins\helper\tagcloud.js
1 2 3 4 5 6
| + const suffix = options.suffix || 'index.html';
result.push( - `<a href="${self.url_for(tag.path)} " style="${style}">${transform ? transform(tag.name) : tag.name}</a>` + `<a href="${self.url_for(tag.path)}${suffix} " style="${style}">${transform ? transform(tag.name) : tag.name}</a>` );
|
NexT 文件修改
Next 主题的更新比 Hexo 频繁,启用 数据目录 后,配置文件的修改不受 NexT 版本更新影响,而源文件的修改每次更新都会被覆盖,需要手动处理。
首页菜单
修改主题配置文件中的 menu
项目,把菜单地址加上 index.html
1 2 3 4 5 6
| menu: home: / || home archives: /archives/index.html || archive categories: /categories/index.html || th tags: /tags/index.html || tags about: /about/index.html || user
|
文章标签
指文章 meta 区的分类
,和文章底部的标签
,NexT-7.x 只需修改 post.swig
,NexT-8.x 需要修改 post-meta.njk
和 post.njk
,如果想了解 NexT-8.x 的变化和安装升级方式,可以参考这篇文章:Hexo-5.x 与 NexT-8.x 跨版本升级
NexT-7.x 版本
修改 \themes\next\layout\_macro\post.swig
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| {% for cat in post.categories %} <span itemprop="about" itemscope itemtype="http://schema.org/Thing">{# - #}<a href="{{ url_for(cat.path) }}" itemprop="url" rel="index">{# + #}<a href="{{ url_for(cat.path) }}index.html" itemprop="url" rel="index">{# #}<span itemprop="name">{{ cat.name }}</span>{# #}</a>{# #}</span>
{% set cat_length = post.categories.length %} {% if cat_length > 1 and loop.index !== cat_length %} {{ __('symbol.comma') }} {% endif %} {% endfor %}
{% if post.tags and post.tags.length and not is_index %} <div class="post-tags"> {% for tag in post.tags %} - <a href="{{ url_for(tag.path) }}" rel="tag"># {{ tag.name }}</a> + <a href="{{ url_for(tag.path) }}index.html" rel="tag"># {{ tag.name }}</a> {% endfor %} </div> {% endif %}
|
NexT-8.x 版本
修改 \node_modules\hexo-theme-next\layout\_partials\post\post-meta.njk
1 2 3 4 5 6 7 8 9 10
| {%- for cat in post.categories.toArray() %} <span itemprop="about" itemscope itemtype="http://schema.org/Thing"> - <a href="{{ url_for(cat.path) }}" itemprop="url" rel="index"><span itemprop="name">{{ cat.name }}</span></a> + <a href="{{ url_for(cat.path) }}index.html" itemprop="url" rel="index"><span itemprop="name">{{ cat.name }}</span></a> </span> {%- set cat_length = post.categories.length %} {%- if cat_length > 1 and loop.index !== cat_length %} {{ __('symbol.comma') }} {%- endif %} {%- endfor %}
|
修改 \node_modules\hexo-theme-next\layout\_macro\post.njk
1 2 3 4 5 6 7 8 9
| {%- if post.tags and post.tags.length %} {%- set tag_indicate = '<i class="fa fa-tag"></i>' if theme.tag_icon else '#' %} <div class="post-tags"> {%- for tag in post.tags.toArray() %} - <a href="{{ url_for(tag.path) }}" rel="tag">{{ tag_indicate }} {{ tag.name }}</a> + <a href="{{ url_for(tag.path) }}index.html" rel="tag">{{ tag_indicate }} {{ tag.name }}</a> {%- endfor %} </div> {%- endif %}
|
全部修改完成后,hexo clean
、hexo g
、hexo s
检查效果