Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
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.
Sam Brown
sambrown.me | welovetxp.com
Offline
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
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?
Sam Brown
sambrown.me | welovetxp.com
Offline
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
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.
Sam Brown
sambrown.me | welovetxp.com
Offline
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
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
Re: Short URLs and rev=canonical
Stef, awesome. Thanks a lot guys! :)
Sam Brown
sambrown.me | welovetxp.com
Offline
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
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
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
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