Replace space in category with - instead

This relates to the url created for a new blog post when the category has multiple words.

I want to either remove the category “How-to Guides” or replace the space with “-” instead of “%20” as per this example (full url removed so it’s not hyperlinked) … /how-to%20guides/2021/05/24/configuring-metamask-for-coston-testnet.html

I have some permalink settings defined in the _config.yml …

permalinks:
    category: /blog/:slugified_categories/:name/
    tag: /blog/tags/:name/
    permalink: /:slugified_categories/:year/:month/:day/:title:output_ext

But they seem to be overridden by the seo.html. Any changes to the above take no effect.

{% if page.canonical_url %}
  {%- assign canonical_url = page.canonical_url %}
{% else %}
  {%- assign canonical_url = page.url | replace: "index.html", "" | absolute_url %}
{% endif %}

Then …

<meta property="og:url" content="{{ canonical_url }}">

Any suggestions on how best to do this please?

I’d say stay with hyphens. Search engines understand them as breaks between words. Underscores are bad here

And %20 is not readable. Some browsers show as a space and some as %20

The Jekyll style is use hyphens and lower case for categories so I’d say don’t that. If you need friendly names for your categories like My Category as a heading and my-category as a label, then that’s worth doing but keep them separate.

Please put your code in triple backticks or indent by 4 spaces to make it a code block.

I think you meant to indent your config to have values under permalink? I don’t know the significance of that. You can set permalink (no plural) as a string in frontmatter, config top level and within categories in config.

Jekyll won’t recognize or process permalinks.

And you are gonna want to use :categories and not slugified categories in the URL.

My apologies, I’ve put the code indented now.

I’ve adjusted the permalink as follows; still no change.

permalink:
category: /blog/:categories/:name/
tag: /blog/tags/:name/
permalink: /:categories/:year/:month/:day/:title:output_ext

Your previous post, agreed I want hyphens only, no space and no %20.

I’m happy to remove the category altogether to resolve this … Thanks for the dev cheatsheets.

It seems to be using the Open Graph settings in the set.html file and not reading the permalink?

Again, this is not valid in Jekyll. Where does permalinks with the data in it come from - a plugin or your code?

Keep things simple. I’d say don’t even set permalink at all in your config. You will already get category and date appearing in the URL.

And add categories like this in your post.

---
title: My title
categories: abc,def-xyz
---

Lowercase and with hyphens.

Result:

_site/abc/def-xyz/2021/02/03/my-post.html

You can list all categories (and tags) on your homepage or a categories page if you want.

Your seo bit will not change the URLs of how pages get outputted. It will only add metadata like title and canonical URL and insert it in your head tag.

The SEO bit I think is unrelated. Get your site to work using no permalink and some sensible category definitions. Turn SEO file off if you must. Then once your URLs are figured out, you can look at the SEO metadata that gets generated. I’d also recommend the Jekyll SEO plugin so you don’t have to manage seo.html yourself in every site of yours.

I removed the permalink from config and there was no change. I’ve just edited the category names to include a hyphen which means categories listed as “Flare-Network” which doesn’t look nice so I’ve removed that.

A workaround yes. I appreciate your assistance.

Hello,

If someone has the solution to replace space in category URL by hyphen, I take it too :slight_smile:

Thanks

Zentor

Look into using the replace filter.

For example:

{%- assign myUrl = "https://hello there world.com/index.com -%}
URL without spaces: {{- myUrl | replace: " ","" -}}

output: https://hellothereworld.com/index.com

Of course you could replace the text with anything, like dashes, underscores, etc.

I don’t think you can do anything about it as far as the permalink with the category name in it is concerned. There is an old issue about this in the repo where I asked about it and they said at the time that they couldn’t slugify the category name in urls as it would not be backwards compatible.

Someone had a clever work around for it using a data file and then you would make the actual category have a dash in it but then make an entry in a datafile with the space, then where ever you used the category you would check the datafile for the cat name with the space.

I just ended up putting dashes in my cat names.