How to remove category prefix from category URLs?

Hey everyone,

is it possible to remove the category prefix from my URLs (for category archives)? I know it’s possible to set state.source.categoryBase, but I would like to remove it completely. Background is that I use Yoast to remove the prefix from the category pages. If I leave categoryBase empty, it says post type from endpoints "posts,pages,media" with slug "mycategory" not found.

Thank you!

Hey @cobra,

The problem you are finding is that once you are not using categoryBase at all, the url of your categories are matching the pattern "/(.*)?/:slug" , which is assigned to the postType handler.

You could try to create a new handler, at the index.js of your theme, with the same pattern but a higher priority where you try the function of the category handler and if it returns an error try the postType. This means it will make a REST API call to check if that slug exists as a category and if not, make the REST API calls for postType. It should be something similar to this:

import Theme from "./components";
import image from "@frontity/html2react/processors/image";

const newHandler = {
  name: "categoryOrPostType",
  priority: 19,
  pattern: "/(.*)?/:slug", 
  func: async ({ route, params, state, libraries }) => {
    // 1. try with category.
    try {
      const category = libraries.source.handlers.find(
        handler => handler.name == "category"
      );
      await category.func({ route, params, state, libraries });
    } catch (e) {
      // It's not a category
      const postType = libraries.source.handlers.find(
        handler => handler.name == "post type"
      );
      await postType.func({ route, params, state, libraries });
    }
  }
};
const marsTheme = {
  name: "@frontity/mars-theme",
  roots: { ... },
  state: { ... },
  actions: { ... },
  libraries: {
    source: {
      handlers: [newHandler]
    },
    ...
  }
};
export default marsTheme;

Please, let us know if you finally make it work or if there’s anything else where we can help.