Baseurl behavior differs between localhost and github pages in Jekyll

岁酱吖の 提交于 2020-02-04 07:29:12

问题


I am developing a static site using Jekyll, to be deployed on github pages. I am facing issues using the baseurl in the configuration file. Here is an extract of my _config.yml:

baseurl: "/blog"
url: "http://remidoolaeghe.github.io"

When ran locally at http://localhost:4000/blog/, everything is fine. The html pages are found, the resources (images, css, js) are loaded and applied on the pages.

Once deployed on Github Pages, I would expect to have the site available at: http://remidoolaeghe.github.io/blog But the actual URL is http://remidoolaeghe.github.io.

It seems the baseurl is not used by the Jekyll on Github Pages. The HTML pages are not at the expected URLs, neither any resources is (css, images, etc). Nothing based on baseurl is loaded by the browser, as you can see here:

I have checked the used Jekyll version. I use the same as Github Pages(2.4.0).

Have I missed something?

My Githubrepo can be found here.


回答1:


Another alternative to redirecting to /blog is:

1. GitHub Pages Redirect from

GitHub pages allow for the redirect-from Jekyll plugin as mentioned here. You could use their plugin and redirect from there. Although as it's built for preventing old links from going to 404, this might not be what you want given your use-case.

2. Manually have meta refresh tag in your root index.html.

<META http-equiv="refresh" content="0;URL=/blog/">

You'll want that tag in a <head> </head>. This will tell the any request to your root url point to the index.html stored at /blog/. This is a fast way to doing it, similar to how .htaccess rewrites are done, except GitHub Pages doesn't support .htaccess for security reasons. At that point also check if you're getting 404s for some resources and update their paths accordingly, maybe with the help of site.baseurl, as another answer explains. baseurl is not what you think it does. See this post by Parker about the baseurl variable.

In short, I think your best option is to use the second one, short of creating a gh-pages blog repository and moving your site there. In fact, if you did move to a a separate blog repo which might make things complicated, you still need to redirect http://remidoolaeghe.github.io to point to http://remidoolaeghe.github.io/blog or else people who visit http://remidoolaeghe.github.io and not the /blog link get a blank page only.




回答2:


The baseurl is used by jekyll to construct url relative to the root of your web site. eg : <link rel="stylesheet" href="{{ "/css/main.css" | prepend: site.baseurl }}">.

It is not used by github page to generate your site path.

In order to reach your blog at http://remidoolaeghe.github.io/blog you need to move it in a blog repository.



来源:https://stackoverflow.com/questions/30209076/baseurl-behavior-differs-between-localhost-and-github-pages-in-jekyll

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!