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?
A dilly of a mod_rewrite pickle
Moderator: Thanas
- Alferd Packer
- Sith Marauder
- Posts: 3706
- Joined: 2002-07-19 09:22pm
- Location: Slumgullion Pass
- Contact:
A dilly of a mod_rewrite pickle
"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.
"Against stupidity the gods themselves contend in vain." - Schiller, Die Jungfrau von Orleans, III vi.
Re: A dilly of a mod_rewrite pickle
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.
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
Vote Electron Standard, vote Tron Paul 2012
- 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
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.
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.
Set him on fire, and he will be warm for life.