A dilly of a mod_rewrite pickle
Posted: 2012-02-14 10:05am
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?
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?