Welcome, Guest. Please login or register.
Did you miss your activation email?
May 26, 2012, 07:49:14 PM

Login with username, password and session length
Search:     Advanced search
Interested in joining the WebsiteBaker team?
For more Information read here or on our new website.
155551 Posts in 21715 Topics by 7737 Members
Latest Member: gx-world
* Home Help Search Login Register
Pages: [1]   Go Down
Print
Author Topic: Snippet: News Redirect url's  (Read 666 times)
crnogorac081
AddOn Development
*
Offline Offline

Posts: 1706



« on: August 18, 2011, 11:55:31 PM »

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

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
« Last Edit: August 28, 2011, 10:14:49 PM by Argos » Logged

Wow, I coded something myself: PM Messanger Modul ,Searchbox with suggestions
Argos
Moderator
**
Offline Offline

Posts: 2161


WWW
« Reply #1 on: August 28, 2011, 10:15:20 PM »

Nice one, Ivan! Thanks for sharing  grin
Logged

Jurgen Nijhuis
Argos Media
Heiloo, The Netherlands
WB Showcase: http://www.mywebsitebaker.com/pages/showcase.php?v&category_id=1242&count=30
----------------------------------------------------------------
Please don't request personal support, use the forums!
Pages: [1]   Go Up
Print
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!