0%

hexo部署在oss

阿里云 OSS 本质是对象储存,没有目录的概念,所以使用 OSS 设置静态网站托管时,地址必须存在对象文件,否则将跳转到首页。比如访问 www.abc.com/a/ ,必须指定为 www.abc.com/a/index.html

Hexo+NexT 环境下,按照以下方式修改,这样在 hexo g 之后生成的静态文件会补全路径。下文在标题中会注明改动的文件属于 Hexo 还是 NexT,遇到版本更新时,需要再次修改相应的源文件。

环境版本

随着 Hexo、Next 版本更新,源文件的内容会发生变化,修改前请检查环境版本是否一致

Hexo: 4.0.0

NexT: 7.5.0

固定链接 | HEXO

修改根目录下的 Hexo 配置文件 ,在 permalink 的最后加上 .html

1
2
3
# URL
permalink: post/:title/index.html
permalink_defaults:

菜单跳转 | NEXT

修改主题配置文件中的 menu 项目,把菜单地址加上 index.html

1
2
3
4
5
6
menu:
home: /index.html || home
about: /about/index.html || user
tags: /tags/index.html || tags
categories: /categories/index.html || th
archives: /archives/index.html || archive

文章标签 | NEXT

修改 \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 %}

分页链接 | HEXO

修改 \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';
};

分类链接 | HEXO

修改 \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;

标签链接 | HEXO

修改 \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;

标签云 | HEXO

修改 \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>`
);

全部修改完成后,hexo cleanhexo ghexo s 检查效果

文章来源:https://www.imczw.com/post/tech/hexo_next_oss_fix.html

参考文章:部署于 OSS 之上的静态博客结构