Automatically add auto-generated short URLs to your sNews CMS

Automatically add auto-generated short URLs to your sNews CMS

This is my first sNews CMS mod/hack to be made public in quite some time, I hope someone finds it useful. This modification will add rel="shortlink" & rel="shorturl" tags to the head of your published items.

What do I mean by published items? Basically all your articles & "pages", not the default category/home page views or hard-coded sNews pages (contact, sitemap, etc). We don't really need/want shortlinks to ever-changing pages (home, category views, etc) anyways.


Unless you've been living under a rock, you've no doubt used or at least heard of Twitter, and the 140 character limitation for Twitter entries. So when posting URLs, people have relied on URL shortening services, such as to reduce the long SEO-Friendly URLs into as few characters as possible. With this mod you can skip using or others services for publishing shortlinks on your own site, for any published items (articles/pages). Another big plus about using these short urls is they never change, as they are based on the unique article IDs from the database. Helps prevent linkrot and eases redirect management.

Adding the code

We will be editing 2 pages for this one, index.php & snews.php. So make a back-up of these files now & let's get started. Now open snews.php and add this function to the very bottom of the file, just above the closing PHP tag;

function redirectShortUrl() {
    $url = trim($_GET['category'],"/"); // fetch & trim the category
    if (ctype_digit($url) && strlen($url) < '7') { // url is a short number, let's process it
        $url = clean($url);
        $query = 'SELECT AS aid,a.seftitle AS asef, c.seftitle AS subsef,x.seftitle AS csef
                FROM  '._PRE.'articles'.' AS a
                LEFT OUTER JOIN '._PRE.'categories'.' as c
                    ON category =
                LEFT OUTER JOIN '._PRE.'categories'.' as x
                    ON c.subcat = AND x.published =\'YES\'
                WHERE a.published = \'1\' AND a.visible = \'YES\' AND ='.$url;
        $result = mysql_query($query); 
        $num_rows = mysql_num_rows($result);
        $row = mysql_fetch_row($result);
        if ($num_rows == '0') {
            $error = true;
        } else {
            if (empty($row[2]) && empty($row[3])) { // found a matching page
                $goto = '/'.$row[1];
            } elseif (!empty($row[2]) && empty($row[3])) { // found a matching article w/cat
                $goto = '/'.$row[2].'/'.$row[1];
            } elseif (!empty($row[2]) && !empty($row[3])) { // found a matching article w/cat & subcat 
                $goto = '/'.$row[3].'/'.$row[2].'/'.$row[1];
        if ($error != true) {
            header ('HTTP/1.1 301 Moved Permanently');
            header ('Location: '.$goto.'');
    } // not a number, pass to snews as-is

That function will sniff out the shortlinks and check for them in the database, perform the redirect if necessary, or simply pass the url on sNews. Now within the function title(), and the following code below the meta keywords output;

$query = 'SELECT id FROM '._PRE.'articles WHERE title = \''.$_TITLE.'\'';
$result = mysql_query($query);
$numrows = mysql_num_rows($result);
if ($numrows == '1') {
    $row = mysql_fetch_row($result);
    echo '<link rel="shortlink" type="text/html" href="'._SITE.$row[0].'" />
    <link rel="shorturl" type="text/html" href="'._SITE.$row[0].'" />';

That little bit of code checks for a matching title and inserts the shortlinks if there is a match. Now lastly, open index.php, and add the redirectShortUrl fucntion just below the snews.php include;


That's it! Now upload your modified files and you should see the shortlinks on your articles & pages now. Clicking a valid shortlink should redirect you to the SEO Friendly URL on your site.


So now you've got short URLs for your pages & articles... what do you do with them? Well obviously you can post the shortlinks by hand in your Twitter/SMS/Facebook posts, but I've also created a simple script that will generate a shortlink for 3rd party services. I use TwitterFeed to automatically post all my articles to Twitter. How? Simply sign-up, add your rss-articles feed to TwitterFeed & link your account to Twitter. Now, to use your own sNews site for short URLs, we have to do a little magic, OK, not magic, we have to convert the URL back to a shortlink for TwitterFeed.

This is how we will do this. Download shortenurl.txt, save it as shortenurl.php, edit the database variables and upload to your server. Now, after you have linked your Twitter, Facebook, etc. accounts to TwitterFeed, go to the TwitterFeed dashboard and do the following;

  1. click the edit button to edit your feed.
  2. click the "Advanced Settings" option.
  3. on the "Shorten link through" dropdown, select "custom"
  4. in the "Custom endpoint" box, type ""

Obviously change above to whatever your domain is. Now whenever you post a new article to your RSS feed, TwitterFeed will pick it up, post it to Twitter and use your own short URL as the linkback.

Caveats & Gotchas

There are 2 major considerations that might cause problems for users;

  1. This script uses the unique IDs from the articles table in the database to generate the shortlinks. This works well, I have allowed up to 6 digits in the code, so unless you've created 1 million articles/pages, you should be fine. However, if you started the MySQL auto-increment at a large number, this could be a problem, but there are work-arounds for that.
  2. Because this script uses the unique table IDs, if you have an article or page with a name of just digits, under 1 million (ex., it will conflict. Do not use this script as it is, or consider adding a letter to your article/page names.

Wrap up

Well, if all goes according to plan (this is my first real post using this new shortlink system), then I should see this article posted to twitter soon, with a linkback containing my domains shortlink. As always, any questions or comments, feel free to leave a comment.



You might like


Hi Matt,
great aritcle and script. I've been doing a little read up on this and somehow all the articles I came across also recommend adding a HTTP:Link to your header with the short link in it, e.g. header('Link: ; rel="shortlink"');
What do you think about that?

Hi seven,

Definitely a good idea to do that as well, I will look into as time permits.


Hi Matt,

Do you know if there's a way rel="shorturl" can validate for html5?
It fails with this error:

Bad value shorturl for attribute rel on element link: Keyword shorturl is not registered

I can't seem to find a way around it.


Hi Morten,

try using data-rel

<link data-rel="shorturl" href="" />

where i have to put

echo 'Permalink: <a href="'._SITE.$r['aid'].'" />'._SITE.$r['aid'].'</a>';

to show under each article and befor comment?

Comments are closed. No new comments allowed.

Copyleft 2002 - 2017 Matt Jones
Hand crafted with HTML5 & CSS3
↑ Back to top