Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2018-03-08 18:09:04

catonine
New Member
Registered: 2018-03-02
Posts: 9

URL's like section/cat1/title

how can I make links like “section/cat1/title”?

Is there any way?

Offline

#2 2018-03-08 20:49:03

code365
Member
From: California
Registered: 2009-08-16
Posts: 110
Website

Re: URL's like section/cat1/title

You want the url to display the section/cat1/title?

Last edited by code365 (2018-03-08 20:50:48)

Offline

#3 2018-03-08 21:13:20

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,615
Website

Re: URL's like section/cat1/title

You can create links like that quite simply:

<a href="<txp:site_url /><txp:section />/<txp:category1 />/<txp:article_url_title />"><txp:title /></a>

… but making Textpattern then show the right page when you click on the link is a different matter altogether.

You could use your .htaccess file to rewrite out the /category1 part of the link but you will need to update it when you add new categories. For example, something like this:

RewriteRule ^([^/]+)/(cat-name-1|cat-name-2|cat-name-3)/([^/]+)?/?$  http://www.yourdomain.com/$1/$3 [L]

You add this line to your .htaccess file in the root directory of your textpattern installation at around this point in the file. If you can’t see the file, you need to switch on show hidden files in your FTP program. This line essentially says take all incoming links to something/one-of-my-listed-category-names/url-title to something/url-title in the background. It stays the same in the browser address line but uses the regular section/url-title scheme.

If you add new categories, or you change their names, you need to add/edit the category names in (cat-name-1|cat-name-2|cat-name-3) to ensure those links are recognised.


TXP Builders – finely-crafted code, design and txp

Offline

#4 2018-03-08 21:16:08

catonine
New Member
Registered: 2018-03-02
Posts: 9

Re: URL's like section/cat1/title

code365 wrote #309769:

You want the url to display the section/cat1/title?

I want that when clicking on a link with url: site/section/cat1/title I would see article with
section:section
Category1=cat1
Title=title

Is that possible?

Offline

#5 2018-03-08 21:31:41

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,615
Website

Re: URL's like section/cat1/title

catonine wrote #309772:

I want that when clicking on a link with url: site/section/cat1/title
I would see article with
section:section
Category1=cat1
Title=title

See above. Just one more note on that: Sections divide content into separate areas while categories classify content. They are not the same as sub-sections, though you can make it look like that. That means that:

  • www.mydomain.com/food/meat/introduction
  • www.mydomain.com/food/fish/introduction

… where food is the section and meat and fish are categories are the same page and will clash as both will be treated as:

  • www.mydomain.com/food/introduction

If you use the above method, you need to be aware of that.


TXP Builders – finely-crafted code, design and txp

Offline

#6 2018-03-09 08:02:16

catonine
New Member
Registered: 2018-03-02
Posts: 9

Re: URL's like section/cat1/title

jakob wrote #309771:

You can create links like that quite simply:

<a href="<txp:site_url /><txp:section />/<txp:category1 />/<txp:article_url_title />"><txp:title /></a>...

… but making Textpattern then show the right page when you click on the link is a different matter altogether.

You could use your .htaccess file to rewrite out the /category1 part of the link but you will need to update it when you add new categories. For example, something like this:

RewriteRule ^([^/]+)/(cat-name-1|cat-name-2|cat-name-3)/([^/]+)?/?$ http://www.yourdomain.com/$1/$3 [L]...

You add this line to your .htaccess file in the root directory of your textpattern installation at around this point in the file. If you can’t see the file, you need to switch on show hidden files in your FTP program. This line essentially says take all incoming links to something/one-of-my-listed-category-names/url-title to something/url-title in the background. It stays the same in the browser address line but uses the regular section/url-title scheme.

If you add new categories, or you change their names, you need to add/edit the category names in (cat-name-1|cat-name-2|cat-name-3) to ensure those links are recognised.

Thanks. But final url in browser show as ‘section/title’ instead ‘section/cat1/title’

I know that this can be done with mod_rewrite, but maybe i’m doing something wrong?

Offline

#7 2018-03-09 09:06:37

Destry
Member
From: Haut-Rhin
Registered: 2004-08-04
Posts: 4,909
Website

Re: URL's like section/cat1/title

I have no idea why URLs are needed like that, but I’m just going to throw this in there as food for thought…

I used to be worried about urls informing organization of content, but the benefit it gives visitors (or makes search engines happy?) is trivial. And unless your site is completely static, structured URLs are somewhat meaningless. As one book author writes about website content, “Every Page Is Page One.” Your metadata is what’s important.

So why not go with cleaner URLs, which povide a lot of benefit in that respect alone: easier to work with in code, easier to read, and if (er when) you need to export/migrate your content, you don’t have to try to replicate the more complex URL structure somewhere else. Redirects are a lot easier.

In a site I’m working on now, I’m using category1 for “indexing” articles into lists and the URLs could be as your talking about, but I’ve decided to just go with super easy: domain.tld/article-title, regardless of section or category. I’m loving it!

Offline

#8 2018-03-09 09:43:07

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,615
Website

Re: URL's like section/cat1/title

catonine wrote #309817:

Thanks. But final url in browser show as ‘section/title’ instead ‘section/cat1/title’

Ah, it’s probably my mod_rewrite directive that’s wrong – the http://www.yourdomain.com/ part is probably resulting in a switched browser URL. If that’s the reason, I rather suspect that rewriting simply to /$1/$3 will produce the same result.

I know it works if you rewrite to index.php?s=$1&id=… but here you have an url-title and not an article id number, and to my knowledge you can’t pass an url-title via an url variable (but maybe I’m wrong?!). You can see other examples that show similar principles here and here but they are not a direct answer to your problem.

I’m sorry but I’m pushed for time, so you’ll need to investigate that further yourself…


TXP Builders – finely-crafted code, design and txp

Offline

#9 2018-03-09 09:58:55

catonine
New Member
Registered: 2018-03-02
Posts: 9

Re: URL's like section/cat1/title

well, I rephrase

I want simply thing: urls like
http://sitename/earth/country/town/city/my_street

Offline

#10 2018-03-09 10:09:52

Destry
Member
From: Haut-Rhin
Registered: 2004-08-04
Posts: 4,909
Website

Re: URL's like section/cat1/title

Yes, we get the concept of creating pseudo organization (information architecture) in your URLs. But you will not easily do it, as you are learning.

So you will either need to abandon that structure, or figure out some complex mod_rewrite. I can’t help you there. Sorry.

Offline

#11 2018-03-09 10:46:53

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,615
Website

Re: URL's like section/cat1/title

catonine wrote #309841:

well, I rephrase

I want simply thing: urls like
http://sitename/earth/country/town/city/my_street

Hmm, and I suspect you want every step of that URL to show the relevant options (I wonder what other options there will be alongside “earth”, or are you joking here? Similarly, how will you differentiate between town and city). If you really want that level of granularity, you’ll need to carefully map out your internal structure first, how you are going to record these pieces of information in your articles and work out how your page templates will respond to each level.

The method I showed above is no use for your desired structure as you’d be constantly updating the RewriteRule to accommodate all the new locations and variants. That is only a practical approach if the site structure remains relatively constant.

First, I suggest you use url variables, e.g. ?country=italy&city=rome&street=via-regia and use rah_gps or adi_gps to retrieve each of these from the URL and convert them into Textpattern variables. Then you will need to develop a series of tag constructions for your page to respond to each combination of variables (e.g. what happens when one is missing).

You can use the variables as attributes for other tags by wrapping them in single quotes, e.g. txp:article_custom category='<txp:variable name="country" />' … /> means process the value of the variable and use it as the category filter. I expect you may find it more useful to store this information as custom fields in your article rather than as deep category tree as it will make it easier for you to construct your custom URLs… otherwise you’ll need to be always recursively determining parent and child categories to construct your urls. You may find with this level of complexity that it is more practical to build direct DB queries directly using smd_query or etc_query than to have numerous article_custom and if_category steps.

And … once you have all that working satisfactorily, you can then build your .htaccess RewriteRule to make clean urls translate to the URL variables, and adapt your custom link-constructing tag accordingly…

Unless “etc” can think of some clever system of recursive nested forms (have a look through the forum for a few recent examples) I can’t think of another good way at the moment.


TXP Builders – finely-crafted code, design and txp

Offline

#12 2018-03-09 11:10:22

etc
Developer
Registered: 2010-11-11
Posts: 5,082
Website GitHub

Re: URL's like section/cat1/title

I suspect that currently txp will not work well with identical article titles within a section, so you’ll need to somehow append ID to your custom URLs:

http://sitename/earth/country/town/city/my_street/22
jakob wrote #309847:

First, I suggest you use url variables, e.g. ?country=italy&city=rome&street=via-regia and use rah_gps or adi_gps to retrieve each of these from the URL and convert them into Textpattern variables.

I agree, it’s easier to start in messy mode and adapt .htaccess once everything is working. In 4.7 <txp:page_url type="country" /> can retrieve URL country (sanitized) value too.

I expect you may find it more useful to store this information as custom fields in your article rather than as deep category tree as it will make it easier for you to construct your custom URLs…

Not necessarily, <txp:breadcrumb /> can be of rescue here, at least in 4.7. For example, this link in an article form

<a href="<txp:site_url /><txp:breadcrumb category='<txp:category1 />' separator="/" wraptag="" label="" link="" />/<txp:article_url_title />/<txp:article_id />">
    <txp:title />
</a>

and this .htaccess rule (before the supplied one)

RewriteRule ^.+/(\d+)$ index.php?id=$1 [L]

should work.

Last edited by etc (2018-03-09 14:22:29)

Offline

Board footer

Powered by FluxBB