web technology is a way of life
Based on an earlier post about short URL issues I decided to update my blog to support it’s own short URL using the wordpress post ID. So now any post on my site can be referenced via a shortURL, this one in fact is http://joelnagy.com/p176.
In order for this to work I added this line to my .htaccess file in the root of my site to set it to redirect the short URL to the actual page (you need the full domain in the rule since WordPress works based on the REQUEST_URI) :
RewriteRule ^/?p(\d+).*$ http://example.com/?p=$1 [QSA,L]
This code is for use in header.php to create the <link> tags that will identify your page’s shorturl. First at the top of the page:
<?php
$this_ID = this_ID();
$shortlinkid = ($this_ID != ''? $this_ID: (isset($_REQUEST['p']) && $_REQUEST['p'] != ''? $_REQUEST['p']: ''));
$uri = $_SERVER['REQUEST_URI'];
if ($uri != '/blog/' && $shortlinkid != '')
$shortlinkid = "/p{$shortlinkid}";
else if ($uri != '/blog/')
$shortlinkid = $uri;
header("Link: <http://example.com{$shortlinkid}>; rel=\"shortlink\"");
?>
Then inside the <head> (near other <link>s):
<link rel="shorturl" href="http://example.com/<?php echo $shortlinkid; ?>" />
<link rel="shortlink" type="text/html" href="http://example.com/<?php echo $shortlinkid; ?>" />
<link rev="canonical" rel="self alternate shorter shorturl shortlink" href="http://example.com/<?php echo $shortlinkid; ?>" />
Here is the this_ID() function that needs to be added to functions.php:
function this_ID($force = false) {
global $wp_query;
if (count($wp_query->posts) == 1 || $force)
return $wp_query->post->ID;
return '';
}
You can also add this code for tweeting the short URL (via chuchcrunch.com):
<!-- This is a link to tweet the post using the new short URL -->
<a rel="nofollow" target="_blank" href="http://twitter.com/home/?status=Just Read : <?php the_title();?> : http://example.com/p<?php the_ID(); ?>">Tweet This Post!</a>
The Livescribe Pulse smartpen gets an app store. This little pen is quite amazing, and it just shows that the business model of an app store is already changing the market for every type of electronic device. So when does my…
The Ghostwire (DSiWare) game looks great and reminds me a lot of Fatal Frame.
FloorPlanner is an interesting bit of software built in Flash that allows you design your home in 2D and view in 3D.
The new Stargate Universe [Philosphocal] teaser looks awesome. I stopped watching Atlantis a bit ago because it got boring, I hope this pumps up the energy as BSG did. It has Lou Diamond Philips and Ming-Na in it, that could make…
Here’s a plethora of well designed RSS Feed icons for free!
Leave a reply