Links without .html extension without the need to create directories
I am running a site in Jekyll. I want to create links like contact
to contact.md
(I am using a link now contact.html
) - this means I want to remove the .html extension. I read this question - How to link a page to page.url without html extension in Jekyll? but it says create a directory for each folder. I don't like this idea - I want to have everything in one main directory. Can you give me any advice?
source to share
It is right. A static site should always have a page to render. Most web servers are configured to search index.html
if you specify a directory.
If you were using an application server, for example Unicorn
and serving dynamic, then you can handle requests, but you want, but web servers like Nginx and Apache will look for files on disk.
Thus:
example.com/contact.html
will actually become:
example.com/contact/index.html
But you can omit the filename and the web server will figure it out:
example.com/contact
source to share
You can customize how Jekyll handles permalinks in any way you want . First, take a look at Jekyll's built-in permalink support , where the format is (for example)
permalink: blah/:title
or
permalink: /:categories/:title
will convince Jekyll that he will start emitting irregular permalinks. Then, to clean up the wasteful structures /foo/index.html
that Jekyll created with this scheme, use something like Will Norris jekyll-clean-urls (or my own jekyll-simple-urls for a more primitive approach). The basic idea is to selectively replace /foo/index.html
on /foo.html
as needed.
module Jekyll
class Post
# Obtain destination path, using clean URLs if requested.
#
# By default, Jekyll will treat /:title permalinks the same as /:title/,
# using a destination file of /:title/index.html. Instead, we change the
# destination file to /:title.html if clean URLs are requested.
def destination_with_clean_urls(dest)
path = destination_without_clean_urls(dest)
path.sub!(/\/index.html$/, '.html') if clean_urls?(permalink)
path
end
alias_method :destination_without_clean_urls, :destination
alias_method :destination, :destination_with_clean_urls
Then you can use MultiView, or just rewrite (or go full hop and serve HTML files without continuing) to make sure the queries /foo
yield /foo.html
. If you are using jekyll serve
or connect
middleware via Yeoman, you need to do a couple of additional tweaks, but that's the idea.