A dilly of a mod_rewrite pickle

GEC: Discuss gaming, computers and electronics and venture into the bizarre world of STGODs.

Moderator: Thanas

Post Reply
User avatar
Alferd Packer
Sith Marauder
Posts: 3706
Joined: 2002-07-19 09:22pm
Location: Slumgullion Pass
Contact:

A dilly of a mod_rewrite pickle

Post by Alferd Packer »

Here's an interesting problem I'm having with one of our LAMP boxes at work. There's a site on it with a bunch of articles whose URLS contain categories and subcategories pertaining to that article. Examples of how the URLS look:

http://www.thissite.com/category1/categ ... e-title-1/
http://www.thissite.com/category2/anoth ... e-title-4/
http://www.thissite.com/category3/categ ... -title-50/

And so on for the 800 or so articles on this site.

So what happened is the people running this site decided to to do away with the categories in their article URLs, and made changes in their CMS to do so. The above articles now resolve as:

http://www.thissite.com/some-article-title-1/
http://www.thissite.com/another-article-title-4/
http://www.thissite.com/yet-another-article-title-50/

Much cleaner of course, but, insodoing, they broke all the old URLs. So now it falls to me to fix it.

So, what I want to do is have a Rewrite rule that will grab the last part of the URL, the article title, thus allowing me to set up a 301 for everyone coming to the old URLs. Where I'm getting stuck is that there is no rhyme or reason to what comes before the article title in the old URLs, and similarly, there's no pattern to the article titles for me to match against. Accordingly, I'm having trouble constructing a general rule that will allow me to grab whatever is between the second-to-last trailing slash and the last trailing slash, and use that to construct my redirect.

I figure there are at least a couple Apache gurus here. Anyone know if this is even possible, or if I'm going to have to construct multiple rules based on the depth of the category structure in the original URL?
"There is a principle which is a bar against all information, which is proof against all arguments and which cannot fail to keep a man in everlasting ignorance--that principle is contempt prior to investigation." -Herbert Spencer

"Against stupidity the gods themselves contend in vain." - Schiller, Die Jungfrau von Orleans, III vi.
User avatar
TronPaul
Padawan Learner
Posts: 232
Joined: 2011-12-05 12:12pm

Re: A dilly of a mod_rewrite pickle

Post by TronPaul »

I don't know much about mod_rewrite, but a Google search shows you could write a regular expression to do it.

http://www.webmasterworld.com/forum92/4332.htm

My regex-fu is not that great (I'd rather not embarrass myself), but if you want I can try and come up with a regular expression to grab the last bit of the url.
If it waddles like a duck and it quacks like a duck, it's a KV-5.
Vote Electron Standard, vote Tron Paul 2012
User avatar
Ariphaos
Jedi Council Member
Posts: 1739
Joined: 2005-10-21 02:48am
Location: Twin Cities, MN, USA
Contact:

Re: A dilly of a mod_rewrite pickle

Post by Ariphaos »

For grabbing the last path element:
RewriteRule /[^/]+/([^/]+)/$ /$1/ [L,R=301]

You'll probably want to create a set of RewriteConds to make that specific enough
RewriteCond %{REQUEST_URI} ^/category
Or something.
Give fire to a man, and he will be warm for a day.
Set him on fire, and he will be warm for life.
Post Reply