<p>I wouldn't normally set up a site like this — but given your setup, the following should work.</p>

<ul>
<li>In the category settings, make sure that your category has 'Entries in this section have their own URLs' checked.</li>
<li>Set your category url format to:

<ul>
<li>top-level: <code>{slug}</code></li>
<li>nested categories: <code>{parent.uri}/{slug}</code></li>
</ul></li>
<li>Set your article entry url format in the channel settings to <code>{category.last.uri}/{slug}</code> (where 'category' is the handle of your category field.)</li>
</ul>

<p>Note the use of <code>last</code>. Categories will be returned as an Element Criteria Model which behaves much like an array (even if there is only one defined). The use of <code>last</code> will return the last category element, which should be the uri pattern that you're looking for.</p>

<p>Of course, if you have other routes in the CMS you will have to be careful about routing conflicts.</p>

<p>As a side note, you might want to consider using a 'structure' instead of 'categories'. I commonly use a single structure that represents all of the top level pages in a site, using <code>Entry Types</code> to distinguish the various field requirements and page layouts. You can then include the appropriate template layout automatically based on 'entry.type'. Likewise, you can use this structure to easily generate your site nav, and be fairly confident you won't run into any routing conflicts. The only reason I mention this is that you seem to already be heading in this direction with your current setup.</p>

<p>Edit: I might also add that you will want to make your category field required, otherwise there will be no route to the article in question.</p>

CC BY-SA 3.0
2016-05-11T02:04:55.350Z
I don't really have the option to change the way the site is set up. We are bring over the old structure to new CMS and really don't want to deal with a ton of 404s & code for 301s hence this route.

I have uploaded some content which only has 1 level content and when I tried the method you mentioned (googled it before and thought I had the right answer, I got Failed Tasks). 

Can you expand more about the structure method? I am still in a phase I have to test the hell out of it before I can start importing 80K html items (thats another headache).
Subrato M
CC BY-SA 3.0
2016-05-11T02:27:33.830Z
What didn't work exactly? What do you mean failed tasks? If you use a structure instead of categories, just use an 'entries' field instead of a 'categories' field. Your structure would match the category structure one-for-one. And you can use it in almost exactly the same way. The only difference is that when you link a structure entry it does not automatically link the parent entries like a category would. Don't know if that's important to you or not.
Douglas McDonald
CC BY-SA 3.0
2016-05-11T02:35:21.680Z
I don't have access to that screenshot of what happens when I set up the url structure as you mentioned. I will take a screenshot as soon as I can and add it. It doesn't show me anything other than Failed Tasks on the left side nav. I assume the failed tasks are basically telling me that the URL structure I chose didn't work when it tried recreating article slugs.

I am not sure if the structure will work or not. I am going to run an import (from csv files) and see what happens. I am not against creating a better way of handling the URL with the same URL structure.
Subrato M
CC BY-SA 3.0
2016-05-11T02:39:36.473Z
Ah.. you must be referring to the import plugin? Might be easier to chat in the craft slack channel for a moment. Feel free to message me there if you like (username is just douglas).
Douglas McDonald
CC BY-SA 3.0
2016-05-11T02:44:53.167Z
Yes, exactly! I couldn't remember the exact details so I didn't want to add unconfirmed information. I will get into chat and send you a message there
Subrato M
CC BY-SA 3.0
2016-05-10T23:57:53.267Z
2016-05-11T00:26:28.550Z
2016-05-11T00:26:28.550Z
Douglas McDonald
<p>I am very new to this CMS and I was able to set it up, import some content from csv files.</p>

<p>I have set up templates and right now if someone clicks on the article, the URL is www.example.com/articles/{slug} of the article. This is all fine if I wasn't migrating from another system and we have a ton of articles and the URL was based on SEO requirements.</p>

<p>I have set up a channel type and one of the fields is Category type. I have 2 more category type fields but I don't use them to create the URL. </p>

<p>Here is the screenshot of what it looks like
<a href="https://i.sstatic.net/PGV86.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/PGV86.png" alt="enter image description here"></a>
A lot of content from olden days doesn't have the second category (so would just belong to Analog/ Mixed Signal ICs) but the newer content (from last 3 odd years most likely has it). So when the Url is display I want the URL to be either www.example.com/Analog_Mixed_Signal_ICs/Title
or 
www.example.com/Analog_Mixed_Signal_ICs/Amplifiers/Title</p>

<p>I am not sure if this simple or complex but I would appreciate if someone can tell me how to achieve this. </p>

<routing><slug>
Custom URL based on Categories
# Custom URL based on Categories

(Asked by: username_0 on 2016-05-06)

I am very new to this CMS and I was able to set it up, import some content from csv files.

I have set up templates and right now if someone clicks on the article, the URL is www.example.com/articles/{slug} of the article. This is all fine if I wasn't migrating from another system and we have a ton of articles and the URL was based on SEO requirements.

I have set up a channel type and one of the fields is Category type. I have 2 more category type fields but I don't use them to create the URL.

Here is the screenshot of what it looks like
[![enter image description here](https://i.sstatic.net/PGV86.png)](https://i.sstatic.net/PGV86.png)
A lot of content from olden days doesn't have the second category (so would just belong to Analog/ Mixed Signal ICs) but the newer content (from last 3 odd years most likely has it). So when the Url is display I want the URL to be either www.example.com/Analog\_Mixed\_Signal\_ICs/Title
or
www.example.com/Analog\_Mixed\_Signal\_ICs/Amplifiers/Title

I am not sure if this simple or complex but I would appreciate if someone can tell me how to achieve this.

**Tags:** routing, slug


---

## 1 answers

### Answer by username_1 on 2016-05-10. Score: 2 | Accepted answer

I wouldn't normally set up a site like this — but given your setup, the following should work.

* In the category settings, make sure that your category has 'Entries in this section have their own URLs' checked.
* Set your category url format to:
  + top-level: `{slug}`
  + nested categories: `{parent.uri}/{slug}`
* Set your article entry url format in the channel settings to `{category.last.uri}/{slug}` (where 'category' is the handle of your category field.)

Note the use of `last`. Categories will be returned as an Element Criteria Model which behaves much like an array (even if there is only one defined). The use of `last` will return the last category element, which should be the uri pattern that you're looking for.

Of course, if you have other routes in the CMS you will have to be careful about routing conflicts.

As a side note, you might want to consider using a 'structure' instead of 'categories'. I commonly use a single structure that represents all of the top level pages in a site, using `Entry Types` to distinguish the various field requirements and page layouts. You can then include the appropriate template layout automatically based on 'entry.type'. Likewise, you can use this structure to easily generate your site nav, and be fairly confident you won't run into any routing conflicts. The only reason I mention this is that you seem to already be heading in this direction with your current setup.

Edit: I might also add that you will want to make your category field required, otherwise there will be no route to the article in question.

> **Comments:**
> **username_0**: I don't really have the option to change the way the site is set up. We are bring over the old structure to new CMS and really don't want to deal with a ton of 404s & code for 301s hence this route.  I have uploaded some content which only has 1 level content and when I tried the method you mentioned (googled it before and thought I had the right answer, I got Failed Tasks).   Can you expand more about the structure method? I am still in a phase I have to test the hell out of it before I can start importing 80K html items (thats another headache).  
> **username_1**: What didn't work exactly? What do you mean failed tasks? If you use a structure instead of categories, just use an 'entries' field instead of a 'categories' field. Your structure would match the category structure one-for-one. And you can use it in almost exactly the same way. The only difference is that when you link a structure entry it does not automatically link the parent entries like a category would. Don't know if that's important to you or not.  
> **username_0**: I don't have access to that screenshot of what happens when I set up the url structure as you mentioned. I will take a screenshot as soon as I can and add it. It doesn't show me anything other than Failed Tasks on the left side nav. I assume the failed tasks are basically telling me that the URL structure I chose didn't work when it tried recreating article slugs.  I am not sure if the structure will work or not. I am going to run an import (from csv files) and see what happens. I am not against creating a better way of handling the URL with the same URL structure.  
> **username_1**: Ah.. you must be referring to the import plugin? Might be easier to chat in the craft slack channel for a moment. Feel free to message me there if you like (username is just douglas).  
> **username_0**: Yes, exactly! I couldn't remember the exact details so I didn't want to add unconfirmed information. I will get into chat and send you a message there  

---

