WebsiteBaker Logo
  • *
  • Templates
  • Help
  • Add-ons
  • Download
  • Home
*
Welcome, Guest. Please login or register.

Login with username, password and session length
 

News


WebsiteBaker 2.13.6 is now available!


Will it continue with WB? It goes on! | Geht es mit WB weiter? Es geht weiter!
https://forum.websitebaker.org/index.php/topic,32340.msg226702.html#msg226702


The forum email address board@websitebaker.org is working again
https://forum.websitebaker.org/index.php/topic,32358.0.html


R.I.P Dietmar (luisehahne) and thank you for all your valuable work for WB
https://forum.websitebaker.org/index.php/topic,32355.0.html


* Support WebsiteBaker

Your donations will help to:

  • Pay for our dedicated server
  • Pay for domain registration
  • and much more!

You can donate by clicking on the button below.


  • Home
  • Help
  • Search
  • Login
  • Register

  • WebsiteBaker Community Forum »
  • WebsiteBaker Support (2.8.x) »
  • Droplets & Snippets »
  • Snippet: News Redirect url's
  • Print
Pages: [1]   Go Down

Author Topic: Snippet: News Redirect url's  (Read 8344 times)

Offline crnogorac081

  • Posts: 2161
  • Gender: Male
Snippet: News Redirect url's
« on: August 19, 2011, 12:55:31 AM »
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: [Select]
<?php
require(&#39;config.php&#39;);

require_once(WB_PATH.&#39;/framework/class.frontend.php&#39;);
$wb = new frontend();
require_once(
WB_PATH.&#39;/framework/frontend.functions.php&#39;);
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:
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, 11:14:49 PM by Argos »
Logged
Web developer

Argos

  • Guest
Re: Snippet: News Redirect url's
« Reply #1 on: August 28, 2011, 11:15:20 PM »
Nice one, Ivan! Thanks for sharing  :-D
Logged

  • Print
Pages: [1]   Go Up
  • WebsiteBaker Community Forum »
  • WebsiteBaker Support (2.8.x) »
  • Droplets & Snippets »
  • Snippet: News Redirect url's
 

  • SMF 2.0.19 | SMF © 2017, Simple Machines
  • XHTML
  • RSS
  • WAP2