Page 1 of 1

My way of changing news pretty url's

Posted: Thu Aug 21, 2008 2:16 pm
by christiaans
Hey guys, as some may have already noticed, I have found a workaround for the news module pretty url's.

Please keep in mind that this is just my personal solution, and may interfere with other modules/options of the news module, and changing files is fully on your own risk (therefore, ALWAYS BACKUP YOUR FILES, unlike I did...  :-[).

Just an example of what we are trying to reach:
(I hate all the capitals in an url, so I'm going for lowercased url's)

You start with: http://www.domain.com/News/1/12/Your-Article-Title-Here

But we are creating: http://www.domain.com/yournewstitle/1/1 ... title-here
--

Okay, here we go, a couple of simple steps will do the trick.

1. Open file News.module.php

Find this lines (around ~line 68 on the unpatched 2.8 module):
$this->RestrictUnknownParams();
$this->RegisterRoute('/[nN]ews\/(?P[0-9]+)\/(?P[0-9]+)\/(?P.*?)\/d,(?P.*?)$/');
$this->RegisterRoute('/[nN]ews\/(?P[0-9]+)\/(?P[0-9]+)\/(?P.*?)$/');
$this->RegisterRoute('/[nN]ews\/(?P[0-9]+)\/(?P[0-9]+)$/');
$this->RegisterRoute('/[nN]ews\/(?P[0-9]+)$/');
$this->RegisterRoute('/[nN]ews\/(?Prss)\/(?P.*?)$/', array('showtemplate'=>'false'));
$this->RegisterRoute('/[nN]ews\/(?Prss)$/', array('showtemplate'=>'false'));
Then replace them with something like these:
(Remember, "yournewstitle", the bold part, can be ANYTHING, it's just an example)
$this->RestrictUnknownParams();
$this->RegisterRoute('/[yY]ournewstitle\/(?P[0-9]+)\/(?P[0-9]+)\/(?P.*?)\/d,(?P.*?)$/');
$this->RegisterRoute('/[yY]ournewstitle\/(?P[0-9]+)\/(?P[0-9]+)\/(?P.*?)$/');
$this->RegisterRoute('/[yY]ournewstitle\/(?P[0-9]+)\/(?P[0-9]+)$/');
$this->RegisterRoute('/[yY]ournewstitle\/(?P[0-9]+)$/');
$this->RegisterRoute('/[yY]ournewstitle\/(?Prss)\/(?P.*?)$/', array('showtemplate'=>'false'));
$this->RegisterRoute('/[yY]ournewstitle\/(?Prss)$/', array('showtemplate'=>'false'));
2. Open up file action.default.php

Find these lines (around ~line 355 on an unpatched 2.8 module):
$aliased_title = munge_string_to_url($row['news_title']);
$prettyurl = 'news/'.$row['news_id'].'/'.($detailpage!=''?$detailpage:$returnid)."/$aliased_title";
Then, replace them with something like these:
(Again remember, "yournewstitle", the bold part, can be ANYTHING, it's just an example)
$aliased_title = munge_string_to_url($row['news_title']);
$aliased_title_stripped = preg_replace("/[^a-z0-9-]/i", "", $aliased_title);
$aliased_title_lowercase = strtolower($aliased_title_stripped);
$prettyurl = 'yournewstitle/'.$row['news_id'].'/'.($detailpage!=''?$detailpage:$returnid)."/$aliased_title_lowercase";
--

Okay, so now we're actually done, but there's more (wow, I just sound like a tellsell narrator ;)), we can add the news date into the url, looks like and comes in handy every now and then I think.

Optional 3. Still in file action.default.php:

Above the following lines (you just added):
$aliased_title = munge_string_to_url($row['news_title']);
$aliased_title_stripped = preg_replace("/[^a-z0-9-]/i", "", $aliased_title);
$aliased_title_lowercase = strtolower($aliased_title_stripped);
$prettyurl = 'yournewstitle/'.$row['news_id'].'/'.($detailpage!=''?$detailpage:$returnid)."/$aliased_title_lowercase";
Add the following:
$news_date = date('d-m-Y', strtotime($row['news_date']));
Then, change your pretty url (see below):
$prettyurl = 'yournewstitle/'.$row['news_id'].'/'.($detailpage!=''?$detailpage:$returnid)."/$aliased_title_lowercase";
To something like:
$prettyurl = 'actueel/'.$row['news_id'].'/'.($detailpage!=''?$detailpage:$returnid)."/$news_date/$aliased_title_lowercase";
This way, you will get the following url:

http://www.domain.com/yournewstitle/11/ ... icle-title

Enjoy, use it as you want, I just thought I'd share it. :)

Re: My way of changing news pretty url's

Posted: Thu Aug 21, 2008 7:31 pm
by hexdj
Would be nice if this was added to the official files, otherwise you have to modify them manually everytime you upgrade.

Re: My way of changing news pretty url's

Posted: Thu Aug 21, 2008 8:06 pm
by christiaans
Very true indeed. That's why I PM'ed Calguy and Ted with it already.

Re: My way of changing news pretty url's

Posted: Tue Sep 02, 2008 6:33 am
by Russ
It would be much better if the date followed the ISO date format and was first as this would make it better for sorting. The internal page identifiers (1/12) could then be last in the url, least important as far as reading goes. e.g. Date/Title/Article/Identifiers

http://www.domain.com/2008-08-19/yourne ... here/1/12/

Seems more logical to me? Which I guess is why other systems use go this way? what do you think?

Russ

Re: My way of changing news pretty url's

Posted: Tue Sep 02, 2008 9:01 am
by christiaans
To be completely honest, I like the date at the end better, but that could very well be personal (as does my client btw). For logic reasons it could be better to place it first, that's true.

Re: My way of changing news pretty url's

Posted: Tue Sep 02, 2008 9:23 am
by Russ
Yes chris-s, many people have different views on this, but from a practical point of view the date first in this format is always the best for date based items like news, or perhaps even events/articles for the Calendar or Blogs module. This is why it is the default on a lot of software out there. I've tried many different ways but keep coming back to this one.

It would be nice if all the modules for CMSMS could follow the same pattern though ;-)

Russ

Re: My way of changing news pretty url's

Posted: Tue Sep 02, 2008 9:54 am
by christiaans
Yes chris-s, many people have different views on this, but from a practical point of view the date first in this format is always the best for date based items like news, or perhaps even events/articles for the Calendar or Blogs module. This is why it is the default on a lot of software out there. I've tried many different ways but keep coming back to this one.

It would be nice if all the modules for CMSMS could follow the same pattern though ;-)

Russ


Obviously it would, but that's the trouble you get with so much different people writing modules ;). As for now; it suits my needs.

Re: My way of changing news pretty url's

Posted: Fri Sep 05, 2008 6:55 am
by Dr.CSS
One way to keep this from being remove on upgrades is to create a new folder in root with something along the lines of Custom module and put your changes there, forgot exactly what it is called learned it long time ago...

Re: My way of changing news pretty url's

Posted: Mon Nov 03, 2008 3:42 pm
by Pierre M.
chris-s wrote: Very true indeed. That's why I PM'ed Calguy and Ted with it already.
Well done. Thx for the pretty URLs.
But may be they prefer the forge to track patches and handle reports. And you can still announce in the forum your posts in the forge.

Have fun hacking CMSms !

Pierre M.

Re: My way of changing news pretty url's

Posted: Tue Dec 29, 2009 3:10 pm
by quantumchris
Just tried the solution posted here and it worked! I wonder why this still hasn't been implemented in the core...

Re: My way of changing news pretty url's

Posted: Tue Dec 29, 2009 3:18 pm
by RonnyK
Dr. CSS wrote: One way to keep this from being remove on upgrades is to create a new folder in root with something along the lines of Custom module and put your changes there, forgot exactly what it is called learned it long time ago...
You mean the \module_custom\News in this case, which could also be used to have your own personal lang-file which isnt overwritten on an upgrade. For News with Dutch labels you could do

\module_custom\News\lang\nl_NL.php with in there, only the specific translated labels (so NOT the complete file). Then only those labels will have the personal translation.

Ronny

CMSMS 1.7 / Pretty URLs for News Module?

Posted: Mon Apr 05, 2010 2:36 am
by WebGirl
Now we're up to CMSMS 1.7, does this still work?

I opened the first file, looked for:
$this->RestrictUnknownParams();
$this->RegisterRoute('/[nN]ews\/(?P[0-9]+)\/(?P[0-9]+)\/(?P.*?)\/d,(?P.*?)$/');
$this->RegisterRoute('/[nN]ews\/(?P[0-9]+)\/(?P[0-9]+)\/(?P.*?)$/');
$this->RegisterRoute('/[nN]ews\/(?P[0-9]+)\/(?P[0-9]+)$/');
$this->RegisterRoute('/[nN]ews\/(?P[0-9]+)$/');
$this->RegisterRoute('/[nN]ews\/(?Prss)\/(?P.*?)$/', array('showtemplate'=>'false'));
$this->RegisterRoute('/[nN]ews\/(?Prss)$/', array('showtemplate'=>'false'));
It seems that the last two lines aren't there any more:
$this->RegisterRoute('/[nN]ews\/(?Prss)\/(?P.*?)$/', array('showtemplate'=>'false'));
$this->RegisterRoute('/[nN]ews\/(?Prss)$/', array('showtemplate'=>'false'));
I'll keep looking in the forums as this was posted some time ago.

Cheers!