My way of changing news pretty url's

Do something cool with CMS? Show us ...
This board is for 'Answers', and the discussion of answers... Not for questions.
Post Reply
christiaans
Power Poster
Power Poster
Posts: 300
Joined: Tue Mar 04, 2008 10:37 am

My way of changing news pretty url's

Post 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. :)
Last edited by Anonymous on Thu Aug 21, 2008 2:20 pm, edited 1 time in total.
hexdj
Power Poster
Power Poster
Posts: 415
Joined: Sat Mar 24, 2007 8:28 am

Re: My way of changing news pretty url's

Post by hexdj »

Would be nice if this was added to the official files, otherwise you have to modify them manually everytime you upgrade.
christiaans
Power Poster
Power Poster
Posts: 300
Joined: Tue Mar 04, 2008 10:37 am

Re: My way of changing news pretty url's

Post by christiaans »

Very true indeed. That's why I PM'ed Calguy and Ted with it already.
Russ
Power Poster
Power Poster
Posts: 813
Joined: Fri Nov 25, 2005 5:02 pm
Location: North West England

Re: My way of changing news pretty url's

Post 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
christiaans
Power Poster
Power Poster
Posts: 300
Joined: Tue Mar 04, 2008 10:37 am

Re: My way of changing news pretty url's

Post 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.
Russ
Power Poster
Power Poster
Posts: 813
Joined: Fri Nov 25, 2005 5:02 pm
Location: North West England

Re: My way of changing news pretty url's

Post 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
christiaans
Power Poster
Power Poster
Posts: 300
Joined: Tue Mar 04, 2008 10:37 am

Re: My way of changing news pretty url's

Post 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.
User avatar
Dr.CSS
Moderator
Moderator
Posts: 12711
Joined: Thu Mar 09, 2006 5:32 am
Location: Arizona

Re: My way of changing news pretty url's

Post 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...
Pierre M.

Re: My way of changing news pretty url's

Post 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.
quantumchris
New Member
New Member
Posts: 2
Joined: Thu Dec 17, 2009 10:39 am

Re: My way of changing news pretty url's

Post by quantumchris »

Just tried the solution posted here and it worked! I wonder why this still hasn't been implemented in the core...
RonnyK
Support Guru
Support Guru
Posts: 4962
Joined: Wed Oct 25, 2006 8:29 pm
Location: Raalte, the Netherlands

Re: My way of changing news pretty url's

Post 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
WebGirl

CMSMS 1.7 / Pretty URLs for News Module?

Post 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!
Last edited by WebGirl on Mon Apr 05, 2010 12:59 pm, edited 1 time in total.
Post Reply

Return to “Tips and Tricks”