PageRenderTime 27ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/docs/_docs/permalinks.md

https://github.com/darwin/jekyll
Markdown | 368 lines | 310 code | 58 blank | 0 comment | 0 complexity | 05e5726b3382fabe082086029b9617da MD5 | raw file
  1. ---
  2. layout: docs
  3. title: Permalinks
  4. permalink: /docs/permalinks/
  5. ---
  6. Permalinks refer to the URLs (excluding the domain name or directory folder) for your pages, posts, or collections.
  7. Jekyll supports a flexible way to build permalinks, allowing you to leverage various template variables or choose built-in permalink styles (such as `date`) that automatically use a template-variable pattern.
  8. You construct permalinks by creating a template URL where dynamic elements are represented by colon-prefixed keywords. The default template permalink is `/:categories/:year/:month/:day/:title.html`. Each of the colon-prefixed keywords is a template variable.
  9. ## Where to configure permalinks
  10. You can configure your site's permalinks through the [Configuration]({% link _docs/configuration.md %}) file or in the [Front Matter]({% link _docs/frontmatter.md %}) for each post, page, or collection.
  11. Setting permalink styles in your configuration file applies the setting globally in your project. You configure permalinks in your `_config.yml` file like this:
  12. ```yaml
  13. permalink: /:categories/:year/:month/:day/:title.html
  14. ```
  15. If you don't specify any permalink setting, Jekyll uses the above pattern as the default.
  16. The permalink can also be set using a built-in permalink style:
  17. ```yaml
  18. permalink: date
  19. ```
  20. `date` is the same as `:categories/:year/:month/:day/:title.html`, the default. See [Built-in Permalink Styles](#builtinpermalinkstyles) below for more options.
  21. Setting the permalink in your post, page, or collection's front matter overrides any global settings. Here's an example:
  22. ```yaml
  23. ---
  24. title: My page title
  25. permalink: /mypageurl/
  26. ---
  27. ```
  28. Even if your configuration file specifies the `date` style, the URL for this page would be `http://somedomain.com/mypageurl/`.
  29. When you use permalinks that omit the `.html` file extension (called "pretty URLs") Jekyll builds the file as index.html placed inside a folder with the page's name. For example:
  30. ```
  31. ├── mypageurl
  32. │   └── index.html
  33. ```
  34. With a URL such as `/mypageurl/`, servers automatically load the index.html file inside the folder, so users can simply navigate to `http://somedomain.com/mypageurl/` to get to `mypageurl/index.html`.
  35. ## Template variables for permalinks {#template-variables}
  36. The following table lists the template variables available for permalinks. You can use these variables in the `permalink` property in your config file.
  37. <div class="mobile-side-scroller">
  38. <table>
  39. <thead>
  40. <tr>
  41. <th>Variable</th>
  42. <th>Description</th>
  43. </tr>
  44. </thead>
  45. <tbody>
  46. <tr>
  47. <td>
  48. <p><code>year</code></p>
  49. </td>
  50. <td>
  51. <p>Year from the post's filename</p>
  52. </td>
  53. </tr>
  54. <tr>
  55. <td>
  56. <p><code>month</code></p>
  57. </td>
  58. <td>
  59. <p>Month from the post's filename</p>
  60. </td>
  61. </tr>
  62. <tr>
  63. <td>
  64. <p><code>i_month</code></p>
  65. </td>
  66. <td>
  67. <p>Month from the post's filename without leading zeros.</p>
  68. </td>
  69. </tr>
  70. <tr>
  71. <td>
  72. <p><code>day</code></p>
  73. </td>
  74. <td>
  75. <p>Day from the post's filename</p>
  76. </td>
  77. </tr>
  78. <tr>
  79. <td>
  80. <p><code>i_day</code></p>
  81. </td>
  82. <td>
  83. <p>Day from the post's filename without leading zeros.</p>
  84. </td>
  85. </tr>
  86. <tr>
  87. <td>
  88. <p><code>short_year</code></p>
  89. </td>
  90. <td>
  91. <p>Year from the post's filename without the century.</p>
  92. </td>
  93. </tr>
  94. <tr>
  95. <td>
  96. <p><code>hour</code></p>
  97. </td>
  98. <td>
  99. <p>
  100. Hour of the day, 24-hour clock, zero-padded from the post's <code>date</code> front matter. (00..23)
  101. </p>
  102. </td>
  103. </tr>
  104. <tr>
  105. <td>
  106. <p><code>minute</code></p>
  107. </td>
  108. <td>
  109. <p>
  110. Minute of the hour from the post's <code>date</code> front matter. (00..59)
  111. </p>
  112. </td>
  113. </tr>
  114. <tr>
  115. <td>
  116. <p><code>second</code></p>
  117. </td>
  118. <td>
  119. <p>
  120. Second of the minute from the post's <code>date</code> front matter. (00..59)
  121. </p>
  122. </td>
  123. </tr>
  124. <tr>
  125. <td>
  126. <p><code>title</code></p>
  127. </td>
  128. <td>
  129. <p>
  130. Title from the documents filename. May be overridden via
  131. the documents <code>slug</code> YAML front matter.
  132. </p>
  133. </td>
  134. </tr>
  135. <tr>
  136. <td>
  137. <p><code>slug</code></p>
  138. </td>
  139. <td>
  140. <p>
  141. Slugified title from the documents filename (any character
  142. except numbers and letters is replaced as hyphen). May be
  143. overridden via the documents <code>slug</code> YAML front matter.
  144. </p>
  145. </td>
  146. </tr>
  147. <tr>
  148. <td>
  149. <p><code>categories</code></p>
  150. </td>
  151. <td>
  152. <p>
  153. The specified categories for this post. If a post has multiple
  154. categories, Jekyll will create a hierarchy (e.g. <code>/category1/category2</code>).
  155. Also Jekyll automatically parses out double slashes in the URLs,
  156. so if no categories are present, it will ignore this.
  157. </p>
  158. </td>
  159. </tr>
  160. </tbody>
  161. </table>
  162. </div>
  163. Note that all template variables relating to time or categories are available to posts only.
  164. ## Built-in permalink styles {#builtinpermalinkstyles}
  165. Although you can specify a custom permalink pattern using [template variables](#template-variables), Jekyll also provides the following built-in styles for convenience.
  166. <div class="mobile-side-scroller">
  167. <table>
  168. <thead>
  169. <tr>
  170. <th>Permalink Style</th>
  171. <th>URL Template</th>
  172. </tr>
  173. </thead>
  174. <tbody>
  175. <tr>
  176. <td>
  177. <p><code>date</code></p>
  178. </td>
  179. <td>
  180. <p><code>/:categories/:year/:month/:day/:title.html</code></p>
  181. </td>
  182. </tr>
  183. <tr>
  184. <td>
  185. <p><code>pretty</code></p>
  186. </td>
  187. <td>
  188. <p><code>/:categories/:year/:month/:day/:title/</code></p>
  189. </td>
  190. </tr>
  191. <tr>
  192. <td>
  193. <p><code>ordinal</code></p>
  194. </td>
  195. <td>
  196. <p><code>/:categories/:year/:y_day/:title.html</code></p>
  197. </td>
  198. </tr>
  199. <tr>
  200. <td>
  201. <p><code>none</code></p>
  202. </td>
  203. <td>
  204. <p><code>/:categories/:title.html</code></p>
  205. </td>
  206. </tr>
  207. </tbody>
  208. </table>
  209. </div>
  210. Rather than typing `permalink: /:categories/:year/:month/:day/:title/`, you can just type `permalink: date`.
  211. <div class="note info">
  212. <h5>Specifying permalinks through the YAML Front Matter</h5>
  213. <p>Built-in permalink styles are not recognized in YAML Front Matter. As a result, <code>permalink: pretty</code> will not work.</p>
  214. </div>
  215. ## Permalink style examples with posts {#permalink-style-examples}
  216. Here are a few examples to clarify how permalink styles get applied with posts.
  217. Given a post named: `/2009-04-29-slap-chop.md`
  218. <div class="mobile-side-scroller">
  219. <table>
  220. <thead>
  221. <tr>
  222. <th>URL Template</th>
  223. <th>Resulting Permalink URL</th>
  224. </tr>
  225. </thead>
  226. <tbody>
  227. <tr>
  228. <td>
  229. <p>None specified, or <code>permalink: date</code></p>
  230. </td>
  231. <td>
  232. <p><code>/2009/04/29/slap-chop.html</code></p>
  233. </td>
  234. </tr>
  235. <tr>
  236. <td>
  237. <p><code>pretty</code></p>
  238. </td>
  239. <td>
  240. <p><code>/2009/04/29/slap-chop/</code></p>
  241. </td>
  242. </tr>
  243. <tr>
  244. <td>
  245. <p><code>/:month-:day-:year/:title.html</code></p>
  246. </td>
  247. <td>
  248. <p><code>/04-29-2009/slap-chop.html</code></p>
  249. </td>
  250. </tr>
  251. <tr>
  252. <td>
  253. <p><code>/blog/:year/:month/:day/:title/</code></p>
  254. </td>
  255. <td>
  256. <p><code>/blog/2009/04/29/slap-chop/</code></p>
  257. </td>
  258. </tr>
  259. <tr>
  260. <td>
  261. <p><code>/:year/:month/:title</code></p>
  262. <p>See <a href="#extensionless-permalinks">Extensionless permalinks with no trailing slashes</a> for details.</p>
  263. </td>
  264. <td>
  265. <p><code>/2009/04/slap-chop</code></p>
  266. </td>
  267. </tr>
  268. </tbody>
  269. </table>
  270. </div>
  271. ## Permalink settings for pages and collections {#pages-and-collections}
  272. The permalink setting in your configuration file specifies the permalink style used for posts, pages, and collections. However, because pages and collections don't have time or categories, these aspects of the permalink style are ignored with pages and collections.
  273. For example:
  274. * A permalink style of `/:categories/:year/:month/:day/:title.html` for posts becomes `/:title.html` for pages and collections.
  275. * A permalink style of `pretty` (or `/:categories/:year/:month/:day/:title/`), which omits the file extension and contains a trailing slash, will update page and collection permalinks to also omit the file extension and contain a trailing slash: `/:title/`.
  276. * A permalink style of `date`, which contains a trailing file extension, will update page permalinks to also contain a trailing file extension: `/:title.html`. But no time or category information will be included.
  277. ## Permalinks and default paths
  278. The path to the post or page in the built site differs for posts, pages, and collections:
  279. ### Posts
  280. The subfolders into which you may have organized your posts inside the `_posts` directory will not be part of the permalink.
  281. If you use a permalink style that omits the `.html` file extension, each post is rendered as an `index.html` file inside a folder with the post's name (for example, `categoryname/2016/12/01/mypostname/index.html`).
  282. ### Pages
  283. Unlike posts, pages by default mimic the source directory structure exactly. (The only exception is if your page has a `permalink` declared its front matter &mdash; in that case, the structure honors the permalink setting instead of the source folder structure.)
  284. As with posts, if you use a permalink style that omits the `.html` file extension, each page is rendered as an `index.html` file inserted inside a folder with the page's name (for example, `mypage/index.html`).
  285. ### Collections
  286. By default, collections follow a similar structure in the `_site` folder as pages, except that the path is prefaced by the collection name. For example: `collectionname/mypage.html`. For permalink settings that omit the file extension, the path would be `collection_name/mypage/index.html`.
  287. Collections have their own way of setting permalinks. Additionally, collections have unique template variables available available (such as `path` and `output_ext`). See the [Configuring permalinks for collections]( ../collections#permalinks ) in Collections for more information.
  288. ## Flattening pages in \_site on build
  289. If you want to flatten your pages (pull them out of subfolders) in the `_site` directory when your site builds (similar to posts), add the `permalink` property to the front matter of each page, with no path specified:
  290. ```
  291. ---
  292. title: My page
  293. permalink: mypageurl.html
  294. ---
  295. ```
  296. ## Extensionless permalinks with no trailing slashes {#extensionless-permalinks}
  297. Jekyll supports permalinks that contain neither a trailing slash nor a file extension, but this requires additional support from the web server to properly serve. When using these types of permalinks, output files written to disk will still have the proper file extension (typically `.html`), so the web server must be able to map requests without file extensions to these files.
  298. Both [GitHub Pages](../github-pages/) and the Jekyll's built-in WEBrick server handle these requests properly without any additional work.
  299. ### Apache
  300. The Apache web server has extensive support for content negotiation and can handle extensionless URLs by setting the [multiviews](https://httpd.apache.org/docs/current/content-negotiation.html#multiviews) option in your `httpd.conf` or `.htaccess` file:
  301. {% highlight apache %}
  302. Options +MultiViews
  303. {% endhighlight %}
  304. ### Nginx
  305. The [try_files](http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files) directive allows you to specify a list of files to search for to process a request. The following configuration will instruct nginx to search for a file with an `.html` extension if an exact match for the requested URI is not found.
  306. {% highlight nginx %}
  307. try_files $uri $uri.html $uri/ =404;
  308. {% endhighlight %}
  309. ## Linking without regard to permalink styles
  310. You can create links in your topics to other posts, pages, or collection items in a way that is valid no matter what permalink configuration you choose. By using the `link` tag, if you change your permalinks, your links won't break. See [Linking to pages](../templates#link) in Templates for more details.