This problem arises because the root directory is always the user url (user.github.com), in both user and project pages. I found a solution to this: Configure the url
variable in the _config.yml
file to the github project page:
safe: true
...
url: "http://user.github.io/project-name"
then, in the layouts, use absolute references, using the site.url
variable to do that:
<link rel="stylesheet" href="{{ site.url }}/css/styles.css">
and run the server using:
$jekyll --server --url=http://localhost:4000
The command line option overwrite the setting in the configuration file in local mode. With those settings, I get all the links pointing to the right place, both hosted in github and in local mode.
UPDATE: Jekyll 1.0 Since Jekyll reached 1.0, the aforemetioned options server
and url
and its respective command line options --server
and --url
have been deprecated. Instructions for the new version:
In the _config.yml
file, add the variable baseurl
(without trailing slash):
baseurl: "http://user.github.io/project-name"
In the layouts or pages, use absolute references, using the site.baseurl
variable:
<link rel="stylesheet" href="{{ site.baseurl }}/css/styles.css">
and then, run the local server (the baseurl option is empty on purpose):
$ jekyll serve --watch --baseurl=
More details about the changes in the docs.
/assets/whatever.js
will work from anywhere. See my answer for more detail. – Ardennes