Snippet: News Redirect url's

Argos

Nice one, Ivan! Thanks for sharing  :-D
Jurgen Nijhuis
Argos Media
Heiloo, The Netherlands
----------------------------------------------------------------
Please don't request personal support, use the forums!

crnogorac081

#1
Hi,

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:

example:
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:

<?php
require('config.php');

require_once(
WB_PATH.'/framework/class.frontend.php');
$wb = new frontend();
require_once(
WB_PATH.'/framework/frontend.functions.php');
if(isset(
$_GET['url'])) { $url =  $wb->add_slashes($_GET['url']); } 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`='$post_id'");
if (
$results->numRows() > 0) {
$row $results->fetchRow();
$redirect_to WB_URL.PAGES_DIRECTORY.$row['link'].PAGE_EXTENSION;
} else {
// Remove .php from link
$link substr('/'.$url0, -4);
$results $database->query("SELECT `link` FROM `".TABLE_PREFIX."pages` WHERE `link`='$link'");
if ($results->numRows() > 0) {
$row $results->fetchRow();
$redirect_to WB_URL.PAGES_DIRECTORY.$row['link'].PAGE_EXTENSION;
} else {
$redirect_to '../index.php';
}
}
Header("Location: '.$redirect_to.'");
?>



in .htaccess file insert this code:


# 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:
none..

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


cheers,
Ivan
Web developer