Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2009-06-16 09:59:52

Sam
Member
From: New York City
Registered: 2004-06-26
Posts: 127
Website

Short URLs and rev=canonical

What would be the best way to approach creating short urls with Textpattern that redirect to the full article URLs (regardless of Permanent link mode)?

So that domain.com/125 redirects to article id 125 but using the full url, which might be say: domain.com/blog/book-review?

These could then be used and auto-generated in the <head> for rev=canonical as a short URL form.

Thanks for thoughts.

Offline

#2 2009-06-16 10:51:54

wet
Developer
From: Lenzing, Austria
Registered: 2005-06-06
Posts: 3,267
Website

Re: Short URLs and rev=canonical

Is this too simple: <link rev="canonical" href="<txp:site_url /><txp:page_url type='id' />" />?

Last edited by wet (2009-06-16 10:54:16)

Offline

#3 2009-06-16 11:00:32

Sam
Member
From: New York City
Registered: 2004-06-26
Posts: 127
Website

Re: Short URLs and rev=canonical

Sure, that’s exactly what I want, but I need that url: site_url/article_id to redirect to the full article URL with permanent links.

So: http://sam.brown.tc/385
Redirects to: http://sam.brown.tc/entry/385/css3-is-here-and-now-lets-use-it

Thus giving people a short url to my articles for use on Twitter or elsewhere without having to use 3rd party sites like bit.ly or tinyurl.

Alas it should work with any type of permanent link mode. Is that even possible?

Offline

#4 2009-06-16 11:21:21

wet
Developer
From: Lenzing, Austria
Registered: 2005-06-06
Posts: 3,267
Website

Re: Short URLs and rev=canonical

For executing the redirections, you could either do the usual cumbersome RewriteRuling, or
put some sniffing code into your error_404 page template which detects numerical shortcuts and redirects accordingly.

Might go like this:

<head>
<txp:php>
$have_id = preg_match('#^/([0-9].*)#', $_SERVER['REQUEST_URI'], $m);
if ($have_id) {
  $id = $m[1];
  $permlink = permlinkurl_id($id);
  if ($permlink) {
    ob_end_clean();
    header("HTTP/1.0 302 Moved Temporarily");
    header("Location: $permlink");
    die();
  }
}
</txp:php>
[...]
</head>

Last edited by wet (2009-06-16 11:24:42)

Offline

#5 2009-06-16 12:11:30

Sam
Member
From: New York City
Registered: 2004-06-26
Posts: 127
Website

Re: Short URLs and rev=canonical

Robert, smashing! This is exactly what I wanted and works perfectly. Thank you so much.

Do you think something like this would be a valuable addition to Textpattern, or perhaps as a simple custom plugin?

Thanks again.

Offline

#6 2009-06-16 17:15:04

wet
Developer
From: Lenzing, Austria
Registered: 2005-06-06
Posts: 3,267
Website

Re: Short URLs and rev=canonical

Sam wrote:

Do you think something like this would be a valuable addition to Textpattern

Depends on the minimum length of all domain names in your inventory, I think ;-)

or perhaps as a simple custom plugin?

Sure. I’m quite positive about Bloke having already fired up his PHP IDE

Offline

#7 2009-06-16 17:56:02

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 10,538
Website GitHub

Re: Short URLs and rev=canonical

wet wrote:

I’m quite positive about Bloke having already fired up his PHP IDE

You mean smd_short_url? ;-) If it flies I’ll tidy it up and make it official. 90% wet’s code of course.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#8 2009-06-16 18:17:03

Sam
Member
From: New York City
Registered: 2004-06-26
Posts: 127
Website

Re: Short URLs and rev=canonical

Stef, awesome. Thanks a lot guys! :)

Offline

#9 2009-06-16 19:24:02

wet
Developer
From: Lenzing, Austria
Registered: 2005-06-06
Posts: 3,267
Website

Re: Short URLs and rev=canonical

Bloke wrote:

You mean smd_short_url? ;-) If it flies

Consider catering for Textpattern living in a sub-directory while matching #^/([0-9].*)#.

Offline

#10 2009-06-16 21:03:13

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 10,538
Website GitHub

Re: Short URLs and rev=canonical

wet wrote:

Consider catering for Textpattern living in a sub-directory while matching #^/([0-9].*)#.

Good call. Slightly trickier than I expected because a regex could potentially match /article/id and think the first bit’s a subdir. In the end the best method was to parse_url() hu and use the path.

Plugin officially released. Note that in keeping with the shortening of stuff, the client tag is now <txp:smd_canonical /> instead of <txp:smd_canonical_url /> :-)


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#11 2009-06-17 03:02:35

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: Short URLs and rev=canonical

Bloke wrote:

Good call. Slightly trickier than I expected because a regex could potentially match /article/id and think the first bit’s a subdir. In the end the best method was to parse_url() hu and use the path.

Just remember that parse_url() is one of PHP’s slowest functions (because the bit huge library, also that differs from lib version to version). Remember not to use it too much ;)

Maybe you could just check the amount of slashes (/). If one, content is numerical and article exists, redirect. You could even do it with super fast string functions.

But it’s kinda the same, as there is only an one instance of parse_url().

Last edited by Gocom (2009-06-17 03:18:17)

Offline

#12 2009-06-17 05:52:11

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 10,538
Website GitHub

Re: Short URLs and rev=canonical

Gocom wrote:

Just remember that parse_url() is one of PHP’s slowest functions

I didn’t know that, thanks for the tip. Perhaps I’ll use one of the faster string functions as you suggest. I’ll see if I can make it robust enough by counting slashes. Cheers, man.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

Board footer

Powered by FluxBB