How to build blog into _site with correct URLs and links for the actual server

Hi all,

I have a situation where there is an existing website, and I need to put a new Jekyll blog in

I have been previewing my blog using jekyll build; jekyll serve and the blog appears at (and http://localhost:4000 also works).

I know that the blog is generated into the _site folder by default. The Jekyll tutorials (e.g. this one) say that the _site folder is used as a “staging area” where I can then copy it over to the server.

I have tried copying over the _site folder into where is located. I put it inside a new folder, blog inside the directory where the other files are stored for this website. However, the problem is that the links are not working. For instance, I’ve had to go through a lot of files and replace:

  • Replace / with /blog/
  • Replace http://localhost:4000 with /blog

This seems too manual. I could do this with a script, but it’s also a bit hacky and it’s hard to figure out how to do the first replacement, since I don’t know how to clearly describe when / should be replaced with /blog/.

My question: when generating the site locally, how can I automatically get the _site folder to include the correct links, so that getting it deployed and located in is as simple as copying the _site folder? I know there are baseurl, url, and similar arguments in the _config.yml files. Unfortunately, I’ve tried modifying those but still, http://localhost:4000 constantly appears in the URLs when I build locally. For instance, when I assign baseurl: "/blog", the website is generated in but the _site folder still includes http://localhost:4000 inside its links (which turn out to be http://localhost:4000/blog but that’s not the behavior I need…) which requires me to manually (or maybe programmatically) delete that.

I appreciate any assistance, and also let me know if there are tutorials about this that I can read. The most relevant one seems to be the deployment methods but the only thing I seem to learn from that is to copy over the _site folder like I’ve been doing.

EDIT 1: to be clear on how I’m copying things over, let’s suppose the website lives in the directory /project/www-ex on some computer/server. This www-ex directory contains a bunch of files (e.g. html ones) that show up on and What I tried to do is copy _site into a new directory /project/www-ex/blog where now, blog is the directory which contains the contents of _site files describing the Jekyll blog.

EDIT 2: I tried following the advice here to change the URL with the production environment, but I seem to get:

$ JEKYLL_ENV=production bundle exec jekyll build
Could not locate Gemfile or .bundle/ directory

This must be because I’m using a blog with an older version of jekyll even though my current version is 3.4.3.

i think you just need to use baseurl in the config file, and use that in all your links.

There is also some stuff in jekyll v 3.3 (I think) where locally using jekyll serve it will not use the baseurl cause it is guessing you are doing local dev and the baseurl setting usually breaks locally. When you do jekyll build it will use the baseurl setting.

Thanks! I’ve indeed been reading that. The blog post seems to be cut off at the end, unfortunately.

What I tried doing is this: I made a new directory with jekyll new blogname. Then I copied the files over from the old directory into this one. Basically, it’s the same blog except it now has the Gemfile and Gemfile.lock files since the older one was originally built without the latest jekyll version. That way, I can now do bundle exec jekyll serve.

I’ll continue working on this, prepending the baseurl and see what I get.

I think I figured it out! It was basically fixing baseurl and then changing my blog to work with the latest version of jekyll. Then I could run in production mode and now _site contains the correct links!