jekyll: baseurl / base-url: GitHub Pages Project Pages - Relative Links Fail
If you’re not using a CNAME and hosting your Project Page on GitHub Pages it’s served out of a “project-name” subfolder
http://user-name.github.com/project-name
Relative url’s fail:
<a href="/">home</a>
resolves to
http://user-name.github.com/
Another example:
<a href="/blog">blog</a>
resolves to
http://user-name.github.com/blog
when I needed:
http://user-name.github.com/project-name/blog
The workaround I can think of for this is setting a “base-url:” in the _config.yml
then put that in front of all href
, src
, url
paths … need it in .css and .js files, so they need to run through Jekyll (see: http://stackoverflow.com/questions/4305955/can-jekyll-act-over-css-or-js-files)… a “base-url:” is suggested in the wiki here: https://github.com/mojombo/jekyll/wiki/Configuration … but I can’t seem to find anything that explains how this should be implemented within your actual files. e.g.
<a href="{{ site.base-url }}/blog">blog</a>
… am I missing something here? Is there a more elegant solution?
About this issue
- Original URL
- State: closed
- Created 13 years ago
- Comments: 38 (11 by maintainers)
Commits related to this issue
- using baseurl per https://github.com/jekyll/jekyll/issues/332 — committed to erictheise/mctad.js by erictheise 10 years ago
- Adding config.yml Trying to solve the base URL issue, and the solution is shown here, https://github.com/jekyll/jekyll/issues/332#issuecomment-18952908 — committed to danrademacher/rimfireweb by deleted user 10 years ago
- update the relative resource loading as workaround to https://github.com/jekyll/jekyll/issues/332 — committed to barakb/asyncrmi by barakb 10 years ago
- Added _Config.yml Conforms to Jekyll, allows internal links to be made in the form of `{{ site.baseurl }}/path/to/css.css` (As referenced from here http://jekyllrb.com/docs/github-pages/ and https... — committed to SungMatt/UsingWavesToFindExoplanets by SungMatt 9 years ago
- Changed baseurl, relative links, according to Jekyll issue 332 https://github.com/jekyll/jekyll/issues/332#issuecomment-72088429 — committed to riuson/lcd-image-converter by riuson 9 years ago
- Changed baseurl, relative links, according to Jekyll issue 332 https://github.com/jekyll/jekyll/issues/332#issuecomment-72088429 — committed to riuson/lcd-image-converter by riuson 9 years ago
- set baseurl for github pages deployment: https://github.com/jekyll/jekyll/issues/332#issuecomment-18952908 — committed to iominh/bootstrap-jekyll by iominh 9 years ago
- mjswensen baseurl fix https://github.com/jekyll/jekyll/issues/332 — committed to thibautdavoult/blog by deleted user 9 years ago
- Fix baseurl problem see. jekyll/jekyll#332 — committed to sudweb/2016 by DirtyF 9 years ago
- fixed relative link issue between local jekyll server and GitHub deployement (see https://github.com/jekyll/jekyll/issues/332#issuecomment-18952908) — committed to EVERTims/evertims.github.io by PyrApple 8 years ago
- fixed relative link issue between local jekyll server and GitHub deployement (see https://github.com/jekyll/jekyll/issues/332#issuecomment-18952908) — committed to EVERTims/evertims.github.io by PyrApple 8 years ago
- fixed relative link issue between local jekyll server and GitHub deployement (see https://github.com/jekyll/jekyll/issues/332#issuecomment-18952908) — committed to EVERTims/evertims.github.io by PyrApple 8 years ago
- This: http://stackoverflow.com/questions/16316311/github-pages-and-relative-paths led me to this (almost didn't notice it!): https://github.com/jekyll/jekyll/issues/332#issuecomment-18952908 This wor... — committed to Mashweb/mashweb.github.io by tomelam 8 years ago
- Add site.baseurl to subrepo page templates according to https://github.com/jekyll/jekyll/issues/332#issuecomment-18952908 — committed to i2000s/en by i2000s 8 years ago
- Bug: Fix urls based on issue 332 fix. https://github.com/jekyll/jekyll/issues/332. — committed to boykoc/should-i-start-a-startup by deleted user 7 years ago
- see https://github.com/jekyll/jekyll/issues/332 — committed to cd2bit/deafchi.github.io by kratsg 7 years ago
- Is this the fix for the prev/next navigation bar failure? Taken from andrewspiers comment on https://github.com/jekyll/jekyll/issues/332, but unable to test locally — committed to 11011110/blog by 11011110 7 years ago
- Trying to fix nav using baseurl via https://github.com/jekyll/jekyll/issues/332#issuecomment-18952908 — committed to bbgvisualjournalist/circumvention by bbgvisualjournalist 7 years ago
- Fix #52 - Broken Links - When we moved from Custom Domain to Github Project few links were not working - Fixed those links - check https://github.com/jekyll/jekyll/issues/332 to know how this fix wo... — committed to electron0zero/ingenuity17 by electron0zero 7 years ago
I finally figured out the trick, if you’re looking for a solution with the standard URL for GitHub Pages (
username.github.io/project-name/
). Here’s what to do:In
_config.yml
, set thebaseurl
option to/project-name
– note the leading slash and the absence of a trailing slash.Now you’ll need to change the way you do links in your templates and posts, in the following two ways:
When referencing JS or CSS files, do it like this:
{{ site.baseurl }}/path/to/css.css
– note the slash immediately following the variable (just before “path”).When doing permalinks or internal links, do it like this:
{{ site.baseurl }}{{ post.url }}
– note that there is no slash between the two variables.Finally, if you’d like to preview your site before committing/deploying using
jekyll serve
, be sure to pass an empty string to the--baseurl
option, so that you can view everything atlocalhost:4000
normally (without/project-name
getting in there to muck everything up):jekyll serve --baseurl ''
This way you can preview your site locally from the site root on localhost, but when GitHub generates your pages from the gh-pages branch all the URLs will start with
/project-name
and resolve properly.Use the HTML
base
tag and it will work fine.This sucks. I would much rather have a solution that allows the built files to be hosted from either of:
Using the same files. And the only way to do that is relative URLs.
My links to posts currently look like
<a href="{{site.baseurl}}{{post.url}}">{{post.title}}</a>
which seems inelegant and took a while to figure out.Also why is it
base-url
at the command line andbaseurl
in the config file?@mjswensen Because I want the same compiled pages to be usable when hosted via gh-pages or via a custom domain. Also, relative urls work with command-line based link checkers.
If anyone else needs this, here is the code that works:
FYI, the canonicalization in line 2 allows
/dir/
and/dir/index.html
to be treated the same, both resulting inrelative-baseurl
equalling../
I believe relative URLs are a reasonable feature to expect from a static website generator.