如何设置与其文件路径不同的 110 个 URL 路径
我正在构建一个 Eleventy 网站,并从我的 Markdown 文件生成博客文章。不幸的是,我无法为这些博客文章设置 URL 路径,使其不同于文件路径结构。它们似乎以一种我无法绕过的方式同步。
SO 有一个现有问题完美地总结了我的情况。但是,接受的解决方案对我不起作用。这是该问题的一个片段:
我想要什么:
https://example.com/my-blog-post/
posts/my-blog-post.md
我得到的:
https://example.com/posts/my-blog-post/
posts/my-blog-post.md
以下是已接受解决方案的片段:
// posts.json (or whatever your collection name is)
{
// ...
"permalink": "/{{ title | slug }}/"
// ...
}
建议更新一个 json 文件,该文件设置针对特定集合的全局数据(在本例中为 posts)。虽然这确实实现了所需的 URL 路径 https://example.com/my-blog-post/
,但它通过删除 posts
父文件夹破坏了文件目录结构,导致我的所有 .md
文件“溢出”到祖父母文件夹中。
这可能是一个渺茫的机会,但我想知道是否有人通过替代方法取得了成功。我觉得这并不是什么极端情况,因为许多人更喜欢最小的 URL 路径(没有 /posts/
或 /blog
为他们的页面添加前缀)。
非常感谢任何帮助!
I'm building an Eleventy site and am generating blog posts from my markdown files. Unfortunately I am unable to set URL paths for these blog posts such that they differ from their file path structure. They seem to be synced in a way that I can't get around.
There is an existing question on SO that sums up my situation perfectly. However, the accepted solution doesn't work for me. Here's a snippet from that question:
What I want:
https://example.com/my-blog-post/
forposts/my-blog-post.md
What I get:
https://example.com/posts/my-blog-post/
forposts/my-blog-post.md
Here's a snippet from the accepted solution:
// posts.json (or whatever your collection name is)
{
// ...
"permalink": "/{{ title | slug }}/"
// ...
}
This proposes updating a json file that sets global data targeting a specific collection (in this case, posts
). While this does achieve the desired URL path of https://example.com/my-blog-post/
, it breaks the file directory structure by removing the posts
parent folder, causing all my .md
files to "spill out" into the grandparent folder.
This may be a long shot, but I'm wondering if anyone has had any success with an alternative approach. I feel like this isn't too much of an edge case, as many people prefer minimal URL paths (without /posts/
or /blog
prefixing their pages).
Any help is much appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
无论您的输出
_site
文件夹多么“混乱”,因为您无论如何都不应该在其中工作。设置永久链接允许您组织输入代码文件(以 Markdown 形式),同时在站点根目录输出文件。输出_site
文件夹必须直接反映您网站的 URL 结构,因为这是 URL 结构的来源和定义。唯一重要的组织是您的输入 Markdown 文件,可以将其组织在
posts/
文件夹中。您可以将其视为编译代码 - 编译后的输出并不适合人类阅读。同样,您的输出_site
目录应完全由 Eleventy 管理。现在,如果您真的希望输出
_site
文件夹有一个posts
子文件夹,同时也有路径可以从站点的根目录访问,您可以使用重定向/重写,这取决于您托管站点的位置。例如,您可以使用 Netlify 的重定向和重写 来重写https://example.com/my-blog-post/
到https://example.com/posts/my-blog-post/
中的内容。然而,这并不是真正为此目的而设计的,因为您的页面可以在两个位置访问,如果做得不当,这可能不利于 SEO,并且会让用户感到困惑。同样,这不是必需的,因为除了调试之外,您不需要查看_site
文件夹。It doesn't matter how "messy" your output
_site
folder is since you shouldn't be working in it anyway. Setting a permalink allows you to organize your input code files (in markdown) while outputting files at the root of your site. The output_site
folder must directly reflect the URL structure of your site since this is the source and definition of your URL structure.The only organization that matters is your input markdown files, which can be organized in a
posts/
folder. You can think of it as compiling code—the compiled output is not meant to be read by humans. Similarly, your output_site
directory should be completely managed by Eleventy.Now, if you really really want to have your output
_site
folder have aposts
subfolder, while also having paths be accessible from the root of your site, you can use redirects/rewrite, which will depend on where you're hosting your site. For example, you can use Netlify's redirects and rewrites to rewritehttps://example.com/my-blog-post/
to content inhttps://example.com/posts/my-blog-post/
. However, this isn't really designed for this purpose, since your page will be accessible at both locations which can be bad for SEO and confusing for users if not done properly. Again, this is not necessary since you should not need to look into your_site
folder besides maybe for debugging.