Forum Moderators: phranque

Message Too Old, No Replies

mod_rewrite and title in the URL

mod_rewrite and title in the URL

         

ceglobal

10:09 am on Jan 27, 2005 (gmt 0)

10+ Year Member



Dear all,

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&amp;file=article&amp;sid=([0-9]*)'",
"'(?<!/)modules.php\?name=News&file=article&sid=([0-9]*)'",

"article\\1.html",

Thank you very much in advance.

Maria

mincklerstraat

12:27 pm on Jan 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to webmasterworld, 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;

under these lines, paste:
$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&amp;file=article&amp;sid=([^\"\']*)'",
"'(?<!/)modules.php\?name=News&file=article&sid=([^\"\']*)'",

This will probably work. I'm not sure what all this business is doing at the beginning of your replace lines: "'(?<!/) . If it doesn't work, try the following instead as those lines:

"'(?<!/)modules.php\?name=News&amp;file=article&amp;sid=([\sA-Za-z0-9_\-\&\;\:\/\$\[\]\(\)\*\^]*)'",
"'(?<!/)modules.php\?name=News&file=article&sid=([\sA-Za-z0-9_\-\&\;\:\/\$\[\]\(\)\*\^]*)'",

If you have to use these two extra lines instead, there's some extra things you need to take into account:
In the part between the square brackets [], the last bit is just a list of all the non alpha-numeric characters you want to match - like ones that you might have in your title. Add to it characters you need, always preceded by a backslash (\). Don't add double quotes to this list; I wouldn't add single quotes either - it looks like your replacement rules are depending on your HTML using single quotes here, which is strange given PHP nuke, since it usually uses double quotes in HTML and as far as I can tell it's not transforming single quotes. If you have to use these two extra lines instead, having characters in your title that aren't listed here will screw up your links and your site won't work. So check all your titles, or all your pages.

ceglobal

8:19 am on Jan 28, 2005 (gmt 0)

10+ Year Member



Dear mincklerstraat,

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

mincklerstraat

12:24 pm on Jan 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, you did nothing wrong, my fault here, forgot something - change the line:

$morelink = preg_replace('#(sid\=[1-9][0-9]*)#', '${1}'.'-'.preg_quote($title), $morelink);

to:
$morelink = preg_replace('#(sid\=[1-9][0-9]*)#', '${1}'.'-'.preg_quote(str_replace(' ', '-', $title)), $morelink);

Cheers, glad to have been of some help. If it still doesn't work drop a line here and if I don't respond sticky me.

ceglobal

10:39 pm on Jan 28, 2005 (gmt 0)

10+ Year Member



Thank you very much. It's work properly.
Best regards,
Maria

ceglobal

9:16 pm on Feb 14, 2005 (gmt 0)

10+ Year Member



Dear mincklerstraat,

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:


Posted by PETER on Saturday, December 25 @ 12:20:12 CST (136 reads)
(comments? ¦ Read More...¦ Print ¦ Send to a friend ¦ Score: 5)

These problems in the "Print" and "Send to a friend" functions are the following:

(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'

mincklerstraat

9:41 am on Feb 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK Maria, you've probably got a theme or a version of PHP-Nuke that's doing things differently than the one I have here. No problem.

Change this line:
$morelink = preg_replace('#(sid\=[1-9][0-9]*)#', '${1}'.'-'.preg_quote(str_replace(' ', '-', $title)), $morelink);

to:
$morelink = preg_replace('#(article(?:&¦&amp;)sid\=[1-9][0-9]*)#', '${1}'.'-'.preg_quote(str_replace(' ', '-', $title)), $morelink);

Also, replace the broken pipe (¦) after the & with one that isn't broken - straight vertical line character (webmasterworld always replaces pipes with broken pipes).
This should only replace parts of this 'morelink' that have 'article&' or 'article&amp;' before 'sid'. Like usual - if it doesn't work, pm me!

[edited by: jdMorgan at 10:54 pm (utc) on Feb 10, 2010]
[edit reason] Disabled smilies in post [/edit]

ceglobal

4:58 pm on Feb 16, 2005 (gmt 0)

10+ Year Member



mincklerstraat,
Problem solved. Once again thank you very much.
Maria