Generating a SEO URL

Discussions and support about the different URL Rewriting techniques for phpBB.

Moderator: Moderators

Generating a SEO URL

Postby enjoyit » Wed Dec 03, 2008 5:42 pm

Hello,

I have a custom script inside a phpBB page, if i know a topic ID, how can i go about using the phpBB SEO functions to generate the URL?

If there a function like generate_url($topic_id) for example?

Thanks!
enjoyit
 
Posts: 3
Joined: Wed Dec 03, 2008 5:41 pm

Advertisement

Postby dcz » Sat Dec 06, 2008 10:38 am

This could be a solution, when sharing phpBB's code (eg including common.php).

Code: Select all
/**
* generate_topic_url($topic_data = array(), $forum_name = false, $start = 0)
* Example call :
* $topic_data = array('topic_id' => 25, 'topic_type' => POST_NORMAL, 'topic_title' => 'this is a title', 'forum_id' => 5);
* if ($test = generate_topic_url($topic_data)) {
*    $url = $test;
* }
* */
function generate_topic_url($topic_data = array(), $forum_name = false, $start = 0) {
   global $phpbb_root_path, $phpEx, $phpbb_seo, $db;
   $topic_id = !empty($topic_data['topic_id']) ? (int) $topic_data['topic_id'] : false;
   $topic_title = !empty($topic_data['topic_title']) ? censor_text($topic_data['topic_title']) : false;
   $topic_type = isset($topic_data['topic_title']) ? (int) $topic_data['topic_title'] : false;
   $forum_id = !empty($topic_data['forum_id']) ? (int) $topic_data['forum_id'] : false;
   $start = max(0, (int) $start);
   if ($topic_id === false) {
      return false;   
   }
   if (empty($phpbb_seo->seo_url['topic'][$topic_id]) || $forum_id === false) {
      if ($topic_title === false || $topic_type === false || $forum_name === false || $forum_id === false) {
         $sql = "SELECT t.topic_title, t.topic_id, t.forum_id, topic_type, f.forum_name
            FROM " . TOPICS_TABLE . " AS t, " . FORUMS_TABLE . " AS f
            WHERE t.topic_id = $topic_id
            AND f.forum_id = t.forum_id";
         $result = $db->sql_query($sql);
         if ($row = $db->sql_fetchrow($result)) {
            $topic_title = censor_text($row['topic_title']);
            $topic_type = $row['topic_type'];
            $forum_name = $row['forum_name'];
            $forum_id = (int) $row['forum_id'];
         } else {
            return false;
         }
      }
      if ($topic_type == POST_GLOBAL) {
         $phpbb_seo->seo_opt['topic_type'][$topic_id] = POST_GLOBAL;
      }
      $phpbb_seo->seo_url['topic'][$topic_id] = $phpbb_seo->format_url($topic_title);
      if ( empty($phpbb_seo->seo_url['forum'][$forum_id]) ) {
         $phpbb_seo->seo_url['forum'][$forum_id] = $phpbb_seo->set_url($forum_name, $forum_id, $phpbb_seo->seo_static['forum']);
      }
   }
   return append_sid($phpbb_root_path . "viewtopic.$phpEx?f=$forum_id&t=$topic_id" . ( $start ? "&start=$start" : ''));
}


$topic_data should contain the regular topic info, minimum is :
Code: Select all
$topic_data = array('topic_id' => xx);


I did like this to be able to pass all required data to the function in case you already have them available.

For example :
Code: Select all
$topic_data = array('topic_id' => 25, 'topic_type' => POST_NORMAL, 'topic_title' => 'this is a title', 'forum_id' => 5);
if ($test = generate_topic_url($topic_data), 'forum title') {
     $url = $test;
}


will not cost any sql.

It could be more a little more optimized, but should work in many useful cases ;)
Useful links :
SEO Forum || SEO Directory || SEO phpBB || Search
____________________

Liens Utiles :
Forum référencement || Annuaire référencement || Référencement phpBB || Recherche
dcz
Admin
Admin
 
Posts: 21428
Joined: Fri Apr 28, 2006 9:03 pm

Re: Generating a SEO URL

Postby Crysis666 » Sat Nov 17, 2012 2:36 pm

When I pass the topic id to the generate_topic_url function, I get back a url that is missing the forum name and also a post id if any so i will get something like
http://www.example.com/forum/test.html
when it should be
http://www.example.com/forum/subforum/test.html
even though I am passing the topic id and the forum id to the function.

What does $url actually contain, an seo friendly url or a standard phpbb url that is converted with htaccess when viewed? I need the seo friendly url inside $url without the need for .htaccess

Can this be done easily? At the moment I have to construct the url myself using preg_replace with regex patterns applied to the topic title and then paste that to the forum title. This works with most urls but not all.
Crysis666
PR0
PR0
 
Posts: 58
Joined: Sat Jun 14, 2008 3:46 pm

Re: Generating a SEO URL

Postby Crysis666 » Tue Nov 20, 2012 1:11 pm

scrap my question above, I worked it out. I was having a problem with the .htaccess which was giving me an incomplete url when I echoed the results of your function in php. Thanks for the code dcz.
Crysis666
PR0
PR0
 
Posts: 58
Joined: Sat Jun 14, 2008 3:46 pm

Re: Generating a SEO URL

Postby Crysis666 » Wed Nov 21, 2012 10:43 pm

I have altered your function a bit to suit my needs but i still have one small problem, the real forum name is used in the url instead of the one set up in Admin>phpbb-seo>Forum Url Management. For example if I have a forum that is called 'Test Forum' and in Forum Url Management area I have its name set as 'tforum', the url returned by your function above will look like http://www.example.com/forum/test-forum/thistopic-t1.html instead of http://www.example.com/forum/tforum/thistopic-t1.html
I am printing the output to a text file by the way, so .htaccess doesn't get to alter the returned url in your function
Code: Select all
return append_sid($phpbb_root_path . "viewtopic.$phpEx?f=$forum_id&t=$topic_id" . ( $start ? "&start=$start" : ''));

so I altered the return to this
Code: Select all
return append_sid($phpbb_seo->seo_url['forum'][$forum_id] . "/" . $phpbb_seo->seo_url['topic'][$topic_id] . "-t" . $topic_id . ( $start ? "/page$start.html" : '.html'));

but like I said, it doesn't work with forums that have different custom forum url management names. Yes the returned url does redirect to the correct one even though it is the wrong forum name in the address, but I would rather it just be the right url without the need for the redirect. Is there any way to get this to do what I need?
Crysis666
PR0
PR0
 
Posts: 58
Joined: Sat Jun 14, 2008 3:46 pm


Return to phpBB mod Rewrite

 


  • Related topics
    Replies
    Views
    Last post

Who is online

Users browsing this forum: No registered users and 4 guests