RedSocialStream Short URL for Linkback

joomla redsocialstream social-media twitter facebook url-shortening php htaccess

Adding short URLs to RedSocialStream Joomla component posts for better Twitter engagement and linkbacks

RedSocialStream Social Media Integration

RedSocialStream is a great Joomla! component for sharing posts to Facebook, Twitter, LinkedIn, and YouTube. However, when posting articles to Twitter, if the introtext exceeds 140 characters, the component simply truncates at the 140th character with no backlink.

Since I don’t post content that’s only 140 characters, I created a workaround to shorten URLs and add them to tweets. I also wanted to append URLs to Facebook and LinkedIn posts to encourage users to return to my site for additional content.

PHP Code Modification

Open administrator/components/com_redsocialstream/controllers/postfeeds.php

Starting around line 188, replace the existing code with the following modification:

// Start changes
if (strlen($article[0]->introtext) > 140) {
  $url = JURI::base().'/c/'.$tw_array[$t];
  // removing what we don't need.
  $url = str_replace('/administrator/', '', $url);
  $url = str_replace('http://www.', '', $url);
  // check URL length to make room in tweet
  $url_len = strlen($url);
  // We are using 139 since there is a space between the text and URL
  $post_length = 139 - $url_len;
  // Trim post and append the URL
  $add_url = true;
} else {
  $post_length = 140;
  // Trim the post
}
$post_feed = strip_tags(substr($article[0]->introtext,0,$post_length));
if ($add_url == true) { $post_feed.= ' '.$url; }

// Comment out the line below
//$post_feed = strip_tags(substr($article[0]->introtext,0,140));
// End Changes

How It Works

The code checks if the content length exceeds 140 characters. If it does:

  1. Generates a short URL using /c/ followed by the article ID
  2. Cleans up the URL by removing unnecessary parts
  3. Calculates URL length to ensure it fits within 140 characters
  4. Trims the post content to make room for the URL
  5. Appends the URL to the end of the post

The Problem

If you stop here and test a tweet, it will post successfully. However, when users click the link, they’ll receive a 404 error because the /c/ page doesn’t actually exist.

The Solution: .htaccess Redirect

Open the .htaccess file from the root of your Joomla! installation. In the “Begin - Custom Redirects” section, add one of the following redirect rules:

For K2 Component:

RewriteCond %{REQUEST_URI} ^/c/(.*)
RewriteRule ^ /index.php?option=com_k2&view=item&id=%1 [R=302,L]

For Joomla Content Component:

RewriteCond %{REQUEST_URI} ^/c/(.*)
RewriteRule ^ /index.php?option=com_content&view=article&id=%1 [R=302,L]

Result

Now when you post to social media:

  • Twitter: Long posts get truncated with a short URL appended
  • Facebook/LinkedIn: Posts include URLs to drive traffic back to your site
  • Users: Can click links and reach the correct article pages

This solution provides better social media engagement while ensuring users can easily find and read your full content.