WebsiteBaker Community Forum

WebsiteBaker Support (2.8.x) => Droplets & Snippets => Topic started by: crnogorac081 on August 19, 2011, 12:55:31 AM

Title: Snippet: News Redirect url's
Post by: crnogorac081 on August 19, 2011, 12:55:31 AM

to explain the situation:
Atm I am developing news portal. There are live textual transmission for sport games. For example, at first point news title is: Live: Real Madrid vs Barcelona 0 - 0 , and that title will create page (news) link. Now at certain moment, result goes to 1 - 0 and admin changes title, and that changes the page link. But if you are atm on that page and you refresh it, you will get 404 redirect..

This code redirects you to the same page and not to 404 error page:

redirects www.url.xy/../this-is-old-news-title-123.php to www.url.xy/../this-is-new-news-title-123.php

In root create page called redirect.php and insert code:
Code: [Select]

$wb = new frontend();
if(isset($_GET[&#39;url&#39;])) { $url =  $wb->add_slashes($_GET[&#39;url&#39;]); } else { exit(0);}

// Split by . and get latest piece before .php (example: this-is.part-of-news-title-123.php)
$pieces explode("."$url);
// Now we have latest piece before .php (example: part-of-news-title-123)
$smaller $pieces[(count($pieces)-2)];
// Now we split by - an get just the number (123)
$piece explode("-"$smaller);
$post_id $piece[(count($piece)-1)];
// Now ewe get url for redirection by post_id (123)
$results $database->query("SELECT `link` FROM `".TABLE_PREFIX."mod_news_posts` WHERE `post_id`=&#39;$post_id&#39;");
if (
$results->numRows() > 0) {
$row $results->fetchRow();
$redirect_to WB_URL.PAGES_DIRECTORY.$row[&#39;link&#39;].PAGE_EXTENSION;
} else {
// Remove .php from link
$link substr(&#39;/&#39;.$url, 0, -4);
$results $database->query("SELECT `link` FROM `".TABLE_PREFIX."pages` WHERE `link`=&#39;$link&#39;");
if ($results->numRows() > 0) {
$row $results->fetchRow();
$redirect_to WB_URL.PAGES_DIRECTORY.$row[&#39;link&#39;].PAGE_EXTENSION;
} else {
$redirect_to = &#39;../index.php&#39;;
Header("Location: &#39;.$redirect_to.&#39;");

in .htaccess file insert this code:

Code: [Select]
# Rewrite old urls to new ones based on post id
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/(admin|framework|cgi-bin|include|languages|modules|multimedia|account|search|temp|templates/.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ redirect.php?url=$1 [L,QSA]

Known bug:

I hope it will be usefull to others.. It mey be usefull if you have outdated news links..

Title: Re: Snippet: News Redirect url's
Post by: Argos on August 28, 2011, 11:15:20 PM
Nice one, Ivan! Thanks for sharing  :-D