Forum Moderators: phranque
I have a PHPnuke portal and I use mod_rewrite to rewrite requested URLs on the fly.
I need to know how to include the complete title of each articles in the URL of the page that contents this articles.
For instance:
--------------
Title of the article in my portal: Serena Williams finally beat Maria Sharapova
URL of the article: [my-portal.com...]
I would like urls looking like this:
---------------------------------------
Title of the article: Serena Williams finally beat Maria Sharapova
URL of the article:
http://www.my-portal.com/article28-Serena-Williams-finally-beat-Maria-Sharapova.html
I think that I have to add at least one variable to the mod_rewrite. Is this variable?: $title
If so, where is the correct place and how to create the hyphens between each words of the new URL?
This are the fragments of code that I have in .htaccess and header.php. How to change it?
.htaccess
#Articles
RewriteRule ^article([1-9][0-9]*).*
modules.php?name=News&file=article&sid=$1
RewriteRule ^article-topic-([0-9]*).html modules.php?name=News&new_topic=$1
header.php
"'(?<!/)modules.php\?name=News&file=article&sid=([0-9]*)'",
"'(?<!/)modules.php\?name=News&file=article&sid=([0-9]*)'",
"article\\1.html",
Thank you very much in advance.
Maria
I'm afraid this isn't going to be possible in any "neat" fashion. I've even had a look at the source code of PHP Nuke's themes and decided to quit on the spot since this could take a while to unravel.
Your mod_rewrite rule isn't a problem here - normally, I think it should go to the right url no matter what title is attached to the end of it. The only drawback you have here is people who link to you, and misspell the title, will still have a valid link going to your site, and this means search engines will think you have a page with duplicate content. This isn't suchh a big problem, though - the best solution, of course, would be to have your article module check the title, but that would be extra work.
The real trick is getting the script to output these url's. Since you are already 'buffer hacking' it (the stuff you have in header.php), it's slightly tricky, but I think I have a solution for you.
I don't run php_nuke myself, I just have a 7.5 on a local test server for trying out a debugger. I haven't actually tried this solution since I don't really feel like adding all this stuff to all these files, and don't know what else belongs in that header (you haven't posted it here), and don't have time to figure that all out. But if you have any luck, this will work.
In your theme, you have a file called theme.php; in that theme, there's the lines:
function themeindex ($aid, $informant, $time, $title, $counter, $topic, $thetext, $notes, $morelink, $topicname, $topicimage, $topictext) {
global $anonymous, $tipath;
$morelink = preg_replace('#(sid\=[1-9][0-9]*)#', '${1}'.'-'.preg_quote($title), $morelink); In your file header.php change those lines you already have to the following:
"'(?<!/)modules.php\?name=News&file=article&sid=([^\"\']*)'",
"'(?<!/)modules.php\?name=News&file=article&sid=([^\"\']*)'",
"'(?<!/)modules.php\?name=News&file=article&sid=([\sA-Za-z0-9_\-\&\;\:\/\$\[\]\(\)\*\^]*)'",
"'(?<!/)modules.php\?name=News&file=article&sid=([\sA-Za-z0-9_\-\&\;\:\/\$\[\]\(\)\*\^]*)'",
Thank you very much for your swift, detailed and clear answer. I implemented your solution and it works. The only problem is the absence of hyphens between the words in the new URL.
The result is like this:
--------------------------
[my-portal.com...]
Probably I did something wrong.
Thank you very much once again and receive my best regards,
Maria
$morelink = preg_replace('#(sid\=[1-9][0-9]*)#', '${1}'.'-'.preg_quote($title), $morelink);
$morelink = preg_replace('#(sid\=[1-9][0-9]*)#', '${1}'.'-'.preg_quote(str_replace(' ', '-', $title)), $morelink); I have duplicated the access to the news module in my PHP-Nuke portal with your help. The new one is the News module of the index and the older module remains in a "Old Articles" block. That is OK for me.
However, there is a problem in the links of the new News module (in the index) in the "print" and "send to a friend" functions. As you know, in the botton of the box of the articles there are normally two lines with the following text:
(i) Printer friendly page is linked to:
[my-portal...]
Error:
-----
Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /vhosts/my-portal/htdocs/includes/sql_layer.php on line 301
Unknown column 'first prhase of the Title-->Serena' in 'where clause'
Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /vhosts/my-portal/htdocs/includes/sql_layer.php on line 301
You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
(ii) Send to a friend is linked to:
[my-portal...]
Error:
-----
Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /vhosts/my-portal/htdocs/includes/sql_layer.php on line 301
Unknown column 'first prhase of the Title-->Serena' in 'where clause'
$morelink = preg_replace('#(sid\=[1-9][0-9]*)#', '${1}'.'-'.preg_quote(str_replace(' ', '-', $title)), $morelink); $morelink = preg_replace('#(article(?:&¦&)sid\=[1-9][0-9]*)#', '${1}'.'-'.preg_quote(str_replace(' ', '-', $title)), $morelink); [edited by: jdMorgan at 10:54 pm (utc) on Feb 10, 2010]
[edit reason] Disabled smilies in post [/edit]