Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add RESTful type routes in Jekyll

The root of the site http://example.com correctly identifies index.html and renders it. In a similar manner, I want, http://example.com/foo to fetch foo.html present in the root of the directory. The site that uses this functionality is www.zachholman.com. I've seen his code in Github. But still I'm not able to find how it is done. Please help.

like image 780
Prasanna Natarajan Avatar asked Aug 14 '11 11:08

Prasanna Natarajan


2 Answers

This feature is actually available in Jekyll. Just add the following line to your _config.yml:

permalink: pretty

This will enable links to posts and pages without .html extension, e.g.

  • /about/ instead of /about.html
  • /YYYY/MM/DD/my-first-post/ instead of YYYY-MM-DD-my-first-post.html

However, you lose the ability to customize permalinks... and the trailing slash is pretty ugly.

Edit: The trailing slash seems to be there by design

like image 92
pstadler Avatar answered Oct 15 '22 19:10

pstadler


It's actually the server that needs adjusting, not jekyll. Be default, jekyll is going to produces files with .html extensions. There may be a way around that, but it's unlikely that you really want to do go that route. Instead, you need to let your web server know that you want those files served when a URL is called with the file's basename (and no extension).

If your site is served via an Apache web server you can enable the "MultiViews" option. In most cases, you can do that be creating an .htaccess file at your site root with the following line:

Options +MultiViews

With this option enabled, when Apache receives a request for:

http://example.com/foo

It will serve the file:

/foo.html

Note that the Apache server must be setup to allow the option to be set in the htaccess file. If not, you would need to do it in the Apache config file itself. If your site is hosted on another web server, you'll need to look for an equivalent setting.

like image 36
Alan W. Smith Avatar answered Oct 15 '22 20:10

Alan W. Smith