Jump to content

Rubbish at Regexp


Pete
 Share

Recommended Posts

Hi guys

I'm hoping one of you will be able to help me.

I'm converting an old Joomla site to PW and I'll need to redirect the old URL structure to the new as the old structure is all over Google.

I need to change urls like this:

http://www.domain.com/index.php/news/11-latest-news/40-a-news-article.html

to this:

http://www.domain.com/news/latest-news/a-news-article/

The bits that need to disappear are in bold: domain.com/index.php/news/11-latest-news/40-a-news-article.html

Any suggestions?

Something else to take into account is that if an article title has a legitimate number in it like this then it's only the first number that should be removed:

domain.com/index.php/news/11-latest-news/40-5-gold-rings.html

These things always hurt my head and I don't seem to get enough time to learn them - it seems like I only need to know about them once every 3-4 years anyway :)

Forgot to mention this has to go in a .htaccess file, but you knew that anyway ;)

Link to comment
Share on other sites

If you just need to remove those specific parts (i.e. all URLs start with index.php/news/11-latest-news/) then try this:

RewriteRule ^index.php/news/11-latest-news/(?:[0-9]*-)([^\/\.]*).html$ http://www.domain.com/news/latest-news/$1/ [L,R=301]

If that's just one example and you're looking to remove index.php and all sequences of [0-9]*- from beginning of pages, you might have to do something like this instead:

RewriteRule ^index.php/(?:[0-9]*-)?([^\/\.]*)/(?:[0-9]*-)?([^\/\.]*).html$ http://www.domain.com/$1/$2/ [L,R=301]

RewriteRule ^index.php/(?:[0-9]*-)?([^\/\.]*)/(?:[0-9]*-)?([^\/\.]*)/(?:[0-9]*-)?([^\/\.]*).html$ http://www.domain.com/$1/$2/$3/ [L,R=301]

Note: that last one is pretty ugly and requires new rewriterule for each level of content. I'm not aware of any sensible way of implementing string replacement via rewriterules, so if this really is the case you might benefit from actually doing this with PHP -- just point all index.php requests to custom PHP script (or page or whatever) and do 301 redirect from there after a bit of preg_replace() magic.

  • Like 1
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...