以下方案对于 hexo-generator-feed 插件和 hexo-feed 插件通用,不过我目前使用的是hexo-generator-feed

因为之前使用的动态框架,RSS地址是 https://banzhuanriji.com/feed/ ,更换为hexo之后,使用hexo-generator-feed插件,RSS地址变成:

https://banzhuanriji.com/atom.xml
https://banzhuanriji.com/rss.xml

这不是我想要的,我想保持 https://banzhuanriji.com/feed/ 这个地址。首先看官方文档 :https://www.npmjs.com/package/hexo-generator-feed ,配置的格式大概如下:

feed:
  type:
    - atom
    - rss2
  path:
    - atom.xml
    - rss2.xml
  limit: 20
  hub:
  content:
  content_limit: 140
  content_limit_delim: ' '
  order_by: -date
  icon: icon.png
  autodiscovery: true
  template:

也就是说,你是可以自定义地址的。

但是如果你的地址修改为例如 domain.com/feed/ ,它会直接下载这个xml文件而不是打开访问。通过插件配置的话,这里似乎没有什么好的解决方案。

解决方案

我是部署在腾讯云 edgeone 的 pages 服务上的。他是类似vercel的服务。同样也和vercel一样支持一些自定义行文,你只需要创建edgeone.json文件即可。

其中支持redirects和rewrites,下面是我第一次写的文件

{
  "rewrites": [
    {
      "source": "/feed",
      "destination": "/rss.xml"
    }
  ],
  "headers": [
    {
      "source": "/feed",
      "headers": [
        {
          "key": "Content-Type",
          "value": "application/xml; charset=utf-8"
        }
      ]
    }
  ]
}

这里会有一个问题,就是如果访问 https://banzhuanriji.com/feed/ ,是会404的,只可以访问 https://banzhuanriji.com/feed

但是在feed后添加/,系统会认为这是个文件夹,也不可用。所以就要用下面的两个方案:

1. 重定向

{
  "redirects": [
    {
      "source": "/feed/",
      "destination": "/feed",
      "statusCode": 301
    }
  ],
  "rewrites": [
    {
      "source": "/feed",
      "destination": "/rss.xml"
    }
  ],
  "headers": [
    {
      "source": "/feed",
      "headers": [
        {
          "key": "Content-Type",
          "value": "application/xml; charset=utf-8"
        }
      ]
    },
    {
      "source": "/rss.xml",
      "headers": [
        {
          "key": "Content-Type",
          "value": "application/xml; charset=utf-8"
        }
      ]
    }
  ]
}

它的逻辑打开就是之前的重写方案,然后添加一个重定向方案。但是浏览器会提示太多redirects,直接访问失败。

2. 最终方案,捕捉所有feed的地址

{
  "rewrites": [
    {
      "source": "/feed/*",
      "destination": "/rss.xml"
    }
  ],
  "headers": [
    {
      "source": "/feed/*",
      "headers": [
        {
          "key": "Content-Type",
          "value": "application/xml; charset=utf-8"
        }
      ]
    }
  ]
}

这个方案终于可行,但是有一个问题,就是这个方案会匹配所有feed下的请求到你指定的文件。(不过至少可用了😄

如果你有一定的阅读逻辑,你可以点击这里查看文档 https://edgeone.cloud.tencent.com/pages/document/162936771610066944