Search code examples
lektor

changing the URL structure of lektor for blog posts to be in the parent directory


the problem: we are porting a wordpress blog to lektor. The slugs in wordpress of the post are directly under the main domain e.g. www.mypage.com/my-blogpost-about-food/ not www.mypage.com/blog/my-blogpost-about-food/.

Now the owner is not ready to accept any broken links. Broken links from the outside, e.g. some random internet page links to www.mypage.com/my-blogpost-about-food/. This link will not work in lektor. This will decrease the page rank of the redesign of the page with lektor. What can I do to avoid this?

Again, in our current wordpress website

the main blog page sits under:

www.mypage.com/blog/

each blog post has a unique slug, NOT under /blog/ but directly in the main. e.g.

www.mypage.com/my-blogpost-about-food/

What can I do, to make these URLs be valid for lektor as well? that e.g. mypage.com/my-blogpost-about-food is a blog post.

EDIT: this is what lektor does not support. Blog posts in lektor must sit in www.mypage.com/blog/SINGLE-BLOG-POSTS-MUSTBEHERE but I want the blog posts to be here www.mypage.com/SINGLE-BLOG-POSTS-MUSTBEHERE

reading here https://www.getlektor.com/docs/content/urls/

if I go to the system fields and enter my-blogpost-about-food it will still appear in /blog/my-blogpost-about-food/

reading here it becomes clear that I cannot change the slug in the parent model neither:

With the above settings the blog will live at blog/ and the posts at blog/. But what if you want to put the date of the blog post into the URL? That's thankfully very easy. All you need to do is to set up a new URL format for the children. Just edit blog.ini and add this to the [children] section:

slug_format = {{ (this.pub_date|dateformat('YYYY/M/') if this.pub_date) ~ this._id }}

What this does is that it will prepend the year (YYYY) and month (M) to the ID of the page if the publication date is configured. Otherwise it will just use the ID of the page. With this change our blog post will move from for instance blog/hello/ to blog/2015/12/hello/.

now how would I achieve a behaviour similar to this?

this is the formal of the perma links of our wordpress installation: enter image description here


Solution

  • This is possible. If you started with the default setup from lektor quickstart:

    • In models/blog-post.ini, remove hidden = yes
    • In models/blog.ini, under the pagination section, add items = site.query('/').filter(F._model == 'blog-post')

    The first step lets you create blog posts under root in the admin. The second step tells the page blog to look for its pagination items under /, and only include the ones of type blog-post. This will exclude e.g. About, Blog, etc.

    You will now have your site's home page at /, a blog page with list of all posts at /blog, and each post will be located at example.com/my-blogpost-about-food/.

    To create a new blog post in the admin, go to the root admin page (not the /blog admin page), click the +, and select Blog Post for your new page type. It will show up in the list on /blog, and live directly under /.